How to use string as ui->qpushbutton (typecast)
-
Hello everyone,
I would like to inform you that i created a desktop application based on ui(qwidget) implementation.I used many qpushbutton on the gui and i just would like to call it by ui->string name(not q pushbutton name)
For example,
Normally i call pushbutton with ui->camera1 which is qpushbutton name specified in mainwindow.uiBut what i want is,
Define string as Camera1,Camera2,... and so on instead of ui->Camera1 qpushbutton name.But unfortunately "ui->" just recognize qpushbutton names and when i try to call with ui->string name it does not recognize.
İf i can not use the string name instead of the qpushbutton which is defined, i will have to create a seperate loop for each qpushbutton name.
İf i call the button with a string name via ui, I will not need to create a loop for each button.
Is there any method or example to call button with string name(i dont want Qmap method because i have to define a string for only one button).
Ui->stringname as qpushbutton name.
-
@esconda said in How to use string as ui->qpushbutton (typecast):
ui->string name(not q pushbutton name)
This is impossible. The "ui" thingy is a c++ object and by calling
ui->something
you are calling it's member variable. It's not possible to call it via string likeui->"something"
, same as it is impossible to call any other method / member by string in such a way.However, there is another possibility: give your pushbuttons an
objectName
(you can set it programmatically or in Qt Designer) and then you can probably useui->findChild<QPushButton*>("myStringName")
. -
hi @esconda ,
I know of only 1 way to archive that:QPushButton *btn = ui->findChild<QPushButton *>("Stringname"); if(btn) //btn was found else //btn was not found
but, that is a heavy function, and the time to execute this increases significantly with the number of items it has to go through
The arguably better version would be, after
setupUi
go through all items once, and store the Name and a Pointer to the object in a List and later in your code, go through that one instead -
I am trying to call findchold with ui->findchild but unfortunately findchild is not available inside of "ui".ui does not recognize.
I think your answers will work correctly and solve my problem.How can i recognize findchild inside of the ui.(is there any lib to define for it)?
The error is,
C2662: T Qobject::findChild <QPushButton*> cannot convert "this" pointer from Ui::MainWindow to const QObject &
İ have mainwindow class and directly connected to ui.İn the constructor i have ui-setupui(this)
-
Oh it might be that ui is not a QObject in itself. In such case, try findChild not from ui but from your main window.
-
You're welcome, happy coding! :-)
-
@J.Hilk said in How to use string as ui->qpushbutton (typecast):
but, that is a heavy function, and the time to execute this increases significantly with the number of items it has to go through
The arguably better version would be, after setupUi go through all items once, and store the Name and a Pointer to the object in a List and later in your code, go through that one instead.As @J-Hilk says, this is the usual way to code so that you can "look up" an object by a name instead of using the corresponding object directly. Instead of a
QList
, which you have to search to find a name, use aQHash
, and the lookup will be "virtually instantaneous", and will not slow down as the number of buttons/objects gets large, unlike callingfindChild()
each time.Whether you do that or not, I suggest you make your
findChild<Qpushbutton*> (buttonName)
(orQHash::find()
) code into a function and always call that, so that you can easily identify/potentially modify this implementation choice at a later date.