Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to use custom widget with Arguments in QCreator



  • As I know we can use Promote to feature of QTCreator to add a custom widget in QTCreator ui files. For example the following image https://ibb.co/kyN9cH ,we can see the Promote to feature. I have following three files

    1. samplewidget.ui

    <?xml version="2.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>SampleWidget</class>
    <widget class="QWidget" name="SampleWidget">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>SampleWidget</string>
    </property>
    <property name="styleSheet">
    <string notr="true">border-color: rgb(255, 85, 0);</string>
    </property>
    <widget class="QWidget" name="CustomWidget" native="true">
    <property name="geometry">
    <rect>
    <x>170</x>
    <y>90</y>
    <width>120</width>
    <height>80</height>
    </rect>
    </property>
    <property name="styleSheet">
    <string notr="true">background-color: rgb(170, 255, 255);</string>
    </property>
    </widget>
    <widget class="QGroupBox" name="groupBox">
    <property name="geometry">
    <rect>
    <x>160</x>
    <y>70</y>
    <width>241</width>
    <height>141</height>
    </rect>
    </property>
    <property name="title">
    <string>MyBox</string>
    </property>
    </widget>
    </widget>
    <layoutdefault spacing="6" margin="11"/>
    <resources/>
    <connections/>
    </ui>
    2 sampleWidget.cpp

    #include "samplewidget.h"
    #include "ui_samplewidget.h"

    SampleWidget::SampleWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SampleWidget)
    {
    ui->setupUi(this);
    }

    SampleWidget::~SampleWidget()
    {
    delete ui;
    }
    3) samplewidget.h

    #include <QWidget>

    namespace Ui {
    class SampleWidget;
    }

    class SampleWidget : public QWidget
    {
    Q_OBJECT

    public:
    explicit SampleWidget(QWidget *parent = 0);
    

    ~SampleWidget();

    private:
    Ui::SampleWidget *ui;
    };
    Now I want to promote CustomWidget to following myWidget widget

    class myWidget: public QObject {
    Q_OBJECT:
    myWidget((QFrame* frame, QList<QString> = {});
    }
    myWidget::myWidget(QFrame* frame , QList<QString> = {}) {}
    I am not able to do the same . I am looking for some help here


  • Lifetime Qt Champion

    Hi,

    What do you want to do with that list of QString ? (By the way, there's a class called QStringList)

    On a side note, you should have parent as last parameter. It's not mandatory but most if not all Qt users are used to have it like that especially since it has usually a default value of nullptr.



  • It was legacy code , I will make the changes as per your suggestion an it will great if you can tell how to promote my customWidget in my ui file to this newCustimWidget using Qt Creator or any other smarter way

    class neCustomWidget : public QWidget {
    customWidget(QStringList* QFrame* parent>
    }



  • @SGaist

    Qt Enthusiast about 3 hours ago
    It was legacy code , I will make the changes as per your suggestion an it will great if you can tell how to promote my customWidget in my ui file to this newCustimWidget using Qt Creator or any other smarter way

    class neCustomWidget : public QWidget {
    customWidget(QStringList* QFrame* parent>
    }

    I am waiting for your help


  • Qt Champions 2017

    @Qt-Enthusiast said in how to use custom widget with Arguments in QCreator:

    I am waiting for your help

    Have some patience.

    how to promote my customWidget in my ui file to this newCustimWidget using Qt Creator or any other smarter way

    Have you taken a look at the manual?


Log in to reply