Working with different buttons type
Solved
General and Desktop
-
wrote on 1 Oct 2022, 18:56 last edited by
template <typename Control> void AddQLabel(QLabelStruct* qls, Control* control) { QLabel* label = new QLabel(qls->text, control); // <-- this works //.... } void AddQLabel(QLabelStruct* qls, QObject* control) { QLabel* label = new QLabel(qls->text, control); // <-- fails because of control type //... } Main::Main(QWidget* parent) : QMainWindow(parent) { ui.setupUi(this); QObject* btn = qobject_cast<QToolButton*>(ui.toolButton); AddQLabel(&qls, btn); // <-- fail cant use QOBject on new QLabel AddQLabel(&qls, ui.toolButton); // <-- works }
Is it possible to write such a function that could work with button, toolbutton, checkbox, etc
without using a template? -
wrote on 1 Oct 2022, 21:23 last edited by mpergand 10 Jan 2022, 21:25
Because widgets need a QWidget* as parent in their constructors.
-
wrote on 1 Oct 2022, 19:53 last edited by
Got it working using:
void AddQLabel(QLabelStruct* qls, QAbstractButton* control) { }
-
template <typename Control> void AddQLabel(QLabelStruct* qls, Control* control) { QLabel* label = new QLabel(qls->text, control); // <-- this works //.... } void AddQLabel(QLabelStruct* qls, QObject* control) { QLabel* label = new QLabel(qls->text, control); // <-- fails because of control type //... } Main::Main(QWidget* parent) : QMainWindow(parent) { ui.setupUi(this); QObject* btn = qobject_cast<QToolButton*>(ui.toolButton); AddQLabel(&qls, btn); // <-- fail cant use QOBject on new QLabel AddQLabel(&qls, ui.toolButton); // <-- works }
Is it possible to write such a function that could work with button, toolbutton, checkbox, etc
without using a template?wrote on 1 Oct 2022, 20:05 last edited by@n34rt said in Working with different buttons type:
void AddQLabel(QLabelStruct* qls, QObject* control)
use QWidget instead.
-
@n34rt said in Working with different buttons type:
void AddQLabel(QLabelStruct* qls, QObject* control)
use QWidget instead.
-
wrote on 1 Oct 2022, 21:23 last edited by mpergand 10 Jan 2022, 21:25
Because widgets need a QWidget* as parent in their constructors.
1/5