Answer the question
In order to leave comments, you need to log in
Is it possible to implement dynamic addition of menu items in QT?
New problem. Since I have a program for reading data from com ports, I decided that it would be convenient to connect, and not each time enter data into the settings fields, add devices and connection settings to the database, and display the added devices in the toolbar and in the menu.
And accordingly the code starts to look like this:
QSqlQuery query("SELECT * FROM menu");
int fieldNo = query.record().indexOf("title");
while (query.next()) {
QString apparat = query.value(fieldNo).toString();
QByteArray qb =apparat.toUtf8();
char* menu=qb.data();
const QIcon openIcon = QIcon::fromTheme("document-open", QIcon(":/icons/icons8-rs-232-male-40.png"));
QAction *openAct = new QAction(openIcon, tr(menu), this);
ui->toolBar_2->addAction(openAct);
ui->menu->addAction(openAct);
connect(openAct, SIGNAL(triggered()), this, SLOT(slotButton1()));
}
}
Answer the question
In order to leave comments, you need to log in
You don't need to dynamically add slots.
It is enough to connect everything to one slot, and get the sender in it:
In each QAction, put identification information through its methods or through QAction::setProperty and get it in the slot through senderAction->property(..).to..
Instead of QAction, there can be any other sender, the successor of QObject.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question