Signals and slots from runtime-loaded UI-files



  • Good Morning!

    I was just playing with loading UI-files at runtime. I have a menubar with some dropdown menus and some clickable entries. When I click on "Settings", a UI-file will be loaded from the applications resource file and shows up with some buttons and edit fields.

    My question now is:

    How can I implement signals and slots for the elements from the UI-files since I have no *.cpp or *.h files for them?

    Thank you,

    Ceriana


  • Lifetime Qt Champion

    Hi,

    You would have to create a widget containing the slots needed and call QMetaObject::connectSlotsByName on that widget.

    Have a look at the Calculator Builder example

    Hope it helps



  • hi. ui file allow to define connections into itself. e.g a_ui_file.ui:
    @
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>Form</class>
    <widget class="QWidget" name="Form">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>Form</string>
    </property>
    <widget class="QPushButton" name="pushButton">
    <property name="geometry">
    <rect>
    <x>130</x>
    <y>130</y>
    <width>125</width>
    <height>33</height>
    </rect>
    </property>
    <property name="text">
    <string>PushButton</string>
    </property>
    </widget>
    </widget>
    <resources/>
    <!-- Begin below -->
    <connections>
    <connection>
    <sender>pushButton</sender>
    <signal>clicked()</signal>
    <receiver>Form</receiver>
    <slot>close()</slot>
    <hints>
    <hint type="sourcelabel">
    <x>192</x>
    <y>146</y>
    </hint>
    <hint type="destinationlabel">
    <x>199</x>
    <y>149</y>
    </hint>
    </hints>
    </connection>
    </connections>
    </ui>
    @
    Also you can look theese.
    http://qt-project.org/doc/qt-5.1/qtuitools/quiloader.html
    http://qt-project.org/doc/qt-5.1/qtquick/qml-qtquick2-loader.html
    http://qt-project.org/doc/qt-5.1/qtdesigner/qformbuilder.html

    edit: i wonder too.i try it for see whether works or not.



  • [quote author="SGaist" date="1377182562"]Hi,

    You would have to create a widget containing the slots needed and call QMetaObject::connectSlotsByName on that widget.

    Have a look at the Calculator Builder example

    Hope it helps[/quote]

    I dont really understand how to do this. I created a QWidget in the mainwindow-class, but I dont understand what you mean by "containing the slots" and which object to pass to connectSlotsByName.

    The calculator example uses QML, or are there any more calculators I did not see?

    [quote author="unixmania" date="1377183604"]hi. ui file allow to define connections into itself. e.g a_ui_file.ui:
    @
    //
    @
    [/quote]

    Yes, but I want to edit the C++ code for those slots, for example if I do changes on a runtime-loaded QDialog, that I can create a save button in whose slot the data from some QLineEdits gets saved into a configurationfile.



  • it seems that QUiLoader::createWidget() is usable for this job.



  • Sorry, I have no idead how to use it. I looked up QUiLoader::createWidget() in the Qt Assistant, but when I try to get this list, my program crashes:

    @
    void MainWindow::openSettings()
    {
    QUiLoader loader;

    QFile file&#40;":/test.ui"&#41;;
    file.open(QFile::ReadOnly&#41;;
    
    QWidget *wid = loader.load(&file, this);
    *this->widglist = loader.availableWidgets();
    
    wid->show();
    

    }
    @

    Also, I have no idea what this plugin path means, the form is stored inside the resource file.



  • i done it for you ;) main.cpp:
    @
    #include "test.h"
    int main(int argc,char* argv[]){
    QApplication app(argc, argv);
    Ui::Form ui;
    ui.setupUi();
    return app.exec();
    }
    @
    test.h:
    @
    #ifndef TEST_H
    #define TEST_H

    #include <QtCore/QVariant>
    #include <QtWidgets/QAction>
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QButtonGroup>
    #include <QtWidgets/QHeaderView>
    #include <QtWidgets/QPushButton>
    #include <QtWidgets/QWidget>
    #include <QtCore/QFile>
    #include <QtUiTools/QUiLoader>

    QT_BEGIN_NAMESPACE

    class Ui_Form:public QWidget
    {
    Q_OBJECT
    private:

    QUiLoader loader;
    

    signals:

    void call_wid(&#41;;
    

    private slots:

    void emitter(&#41;{
        emit call_wid(&#41;;
    }
    
    void save(&#41;{
        QFile file&#40;"add.ui"&#41;;  // same directory where program in
        QWidget* my=loader.load(&file,this&#41;;
        my->setGeometry(QRect(110,100, 125, 33&#41;&#41;;
        my->show();
    }
    

    public:
    QPushButton *pushButton;

    void setupUi()
    {
        this->setObjectName("Form");
        this->resize(400, 300);
        pushButton = new QPushButton(this);
        pushButton->setObjectName("pushButton");
        pushButton->setGeometry(QRect(110, 20, 125, 33));
        //QWidget *wid = loader.load(&file,Form);
        connect(this, SIGNAL(call_wid()),this,SLOT(save()));
        connect(pushButton, SIGNAL(clicked()),this,SLOT(emitter()));
        this->setWindowTitle("Form");
        pushButton->setText("CALL");
        this->show();
    } // setupUi
    

    };
    namespace Ui {
    class Form: public Ui_Form {};
    } // namespace Ui
    QT_END_NAMESPACE
    #endif // TEST_H
    @
    and test.pro
    @
    TEMPLATE = app
    TARGET = test
    INCLUDEPATH += .
    QT += widgets uitools
    HEADERS += test.h
    SOURCES += main.cpp
    @
    NOTICE: i am on 64bit archlinux desktop with qt 5.1.0
    EDIT: Ahh sorry and add.ui:
    @
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <widget class="QPushButton" name="pushButton">
    <property name="geometry">
    <rect>
    <x>200</x>
    <y>250</y>
    <width>80</width>
    <height>33</height>
    </rect>
    </property>
    <property name="text">
    <string>Save</string>
    </property>
    </widget>
    </ui>
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.