Solved How can I add QTableWidget properties to a custom QWidget designer widget?
-
I've created a custom designer widget that has a QTableWidget and two QPushButton's. I would like the QTableWidget properties and QPushButton properties to be configurable in the custom widgets Qt Designer properties panel. How can I do this?
I thought simply using a Q_PROPERTY macro would work, but it doesn't appear to have any effect as the properties don't appear after I restart Qt with the updated DLL.
Q_PROPERTY(QTableWidget* tableWidget READ GetTableWidget);
Here is the code for the widget, the plugin code is still the default that the wizard created/
EditableWidget.h
#ifndef EDITABLETABLEWIDGET_H #define EDITABLETABLEWIDGET_H #include <QDialog> class QTableWidget; namespace Ui { class EditableTableWidget; } class EditableTableWidget : public QDialog { Q_OBJECT Q_PROPERTY(QTableWidget* tableWidget READ GetTableWidget); public: explicit EditableTableWidget( QWidget *parent = nullptr); ~EditableTableWidget(); private: QString GetBtnText() const; void SetBtnText(QString btnText); QTableWidget* GetTableWidget() const; Ui::EditableTableWidget *ui; }; #endif // EDITABLETABLEWIDGET_H
EditableWidget.cpp
#include "EditableTableWidget.h" #include "ui_EditableTableWidget.h" EditableTableWidget::EditableTableWidget(QWidget *parent) : QDialog(parent), ui(new Ui::EditableTableWidget) { ui->setupUi(this); } EditableTableWidget::~EditableTableWidget() { delete ui; } QString EditableTableWidget::GetBtnText() const { return ui->AddRowBtn->text(); } void EditableTableWidget::SetBtnText(QString btnText) { ui->AddRowBtn->setText(btnText); } QTableWidget* EditableTableWidget::GetTableWidget() const { return ui->TableWidget; }
EditableWidget.h
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>EditableTableWidget</class> <widget class="QDialog" name="EditableTableWidget"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>280</width> <height>240</height> </rect> </property> <property name="windowTitle"> <string>Dialog</string> </property> <widget class="QTableWidget" name="TableWidget"> <property name="geometry"> <rect> <x>10</x> <y>10</y> <width>261</width> <height>192</height> </rect> </property> </widget> <widget class="QPushButton" name="AddRowBtn"> <property name="geometry"> <rect> <x>190</x> <y>210</y> <width>80</width> <height>21</height> </rect> </property> <property name="text"> <string>Add Row</string> </property> </widget> <widget class="QPushButton" name="DeleteRowBtn"> <property name="geometry"> <rect> <x>100</x> <y>210</y> <width>80</width> <height>21</height> </rect> </property> <property name="text"> <string>Delete Row</string> </property> </widget> </widget> <resources/> <connections/> </ui>
-
How should the designer know what to do with a pointer to a Qt class?
You have to expose the properties you want by yourself. -
Re: How can I add QTableWidget properties to a custom QWidget designer widget?
Is there not a way to automatically expose all the available properties when a pointer is provided...?
Hmm, then how can I make it so the user can specify the Column names? For example when you double click a QTableWidget, you can add/remove/edit Columns/Rows/Items.... but if the QTableWidget is in my CustomWidget, this doesn't work (instead the object name field opens up)...
I guess my questions is now... how can I change the double click functionality in the designer for my custom widget to activate the functionality on the table I created within it?
-
I haven't tried that but I'm sure that at least you need to write and build your own Qt designer plugin.
https://doc.qt.io/qt-5/designer-creating-custom-widgets.html -
@KidTrent said in How can I add QTableWidget properties to a custom QWidget designer widget?:
Is there not a way to automatically expose all the available properties when a pointer is provided...?
No, it's not since it's not a common usecase - feel free to provide a patch for designer.
For example when you double click a QTableWidget
Use a proper model instead convenience widgets
-
Great, thanks for the help/clarification!