Solved Working with different buttons type
-
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? -
Because widgets need a QWidget* as parent in their constructors.
-
Got it working using:
void AddQLabel(QLabelStruct* qls, QAbstractButton* control) { }
-
@n34rt said in Working with different buttons type:
void AddQLabel(QLabelStruct* qls, QObject* control)
use QWidget instead.
-
@mpergand thank you, whats the difference?
-
Because widgets need a QWidget* as parent in their constructors.