N
N
Neyury2016-12-29 11:34:15
Qt
Neyury, 2016-12-29 11:34:15

Easiest way to call a function in QT?

When you click on a certain button, you need to perform a certain function (display a modal window, sort, delete, etc.), in general, the simplest event handling.
Do I understand correctly that the easiest way is to create a child class from QPushButton for each button, and define the corresponding slot (function) in it? Or is there an easier way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Толстый Лорри, 2016-12-29
@Neyury

Наследование стоит применять тогда, когда вам нужен доступ к состоянию и детальным событиям. Для обработки нажатия достаточно прицепить ваш обработчик на сигнал QPushButton::clicked:

auto btn = new QPushButton("click me!");

QObject::connect(btn, &QPushButton::clicked, []() { 
// создание диалога и вызов его в модальном режиме (через QDialog::exec)
});
//-----------------------------------
void myFoo() { }

auto btn = new QPushButton("click me!");
QObject::connect(btn, &QPushButton::clicked, myFoo);

UPD: В вашем случае можно сделать примерно так:
auto btn = new QPushButton("click me!");

QObject::connect(btn, &QPushButton::clicked, []() { 
    ::updateTable(table);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question