A
A
AlexSer2019-11-23 17:34:43
Qt
AlexSer, 2019-11-23 17:34:43

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()));
           }
           }

here i have bound the menu items to slotButton1();
But if there are 10 devices, then they are needed accordingly. 10 slots.
That is, the problem I have now is the problem of creating dynamic slots .... Is it possible to create them and how?
To run a signal from the slot to connect each device with its own parameters.
Or is there another way. Just adding menu items for each device, writing slots for each item is somehow not very desirable ..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ighor July, 2019-12-27
@AlexSer

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 question

Ask a Question

731 491 924 answers to any question