[SOLVED]In detial how to using SIGNAL/SOLT to interaction with UI widget in other CPP file?



  • I have creat a Qt gui project,in .ui file had insert some widget like TEXTBOX and LISTWIDGET,then I want interaction with them in other .cpp file.
    And the better way to do this is using SIGNAL/SLOT.Though I already studied it,i still can`t achiece the goal.

    below are my code:

    in mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    public slots:
        void additem(const QString& text);
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    in mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "testclass.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->listWidget->addItem("text");
    
        testclass t;
    }
    
    void MainWindow::additem(const QString& text)
    {
        ui->listWidget->addItem(text);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    in testclass.h

    #ifndef TESTCLASS_H
    #define TESTCLASS_H
    
    #include <QObject>
    #include <mainwindow.h>
    
    class testclass : public QObject
    {
        Q_OBJECT
    public:
        explicit testclass(QObject *parent = 0);
        ~testclass();
    
    signals:
        void add(const QString& text);
    
    public slots:
    
    private:
        MainWindow *mainwindow;
    };
    
    #endif // TESTCLASS_H
    

    in testclass.cpp

    #include "testclass.h"
    
    testclass::testclass(QObject *parent) : QObject(parent)
    {
        connect(this,SIGNAL(add(const QString&)),mainwindow,SLOT(additem(const QString&)));
        emit add("simulation");
    }
    
    testclass::~testclass()
    {
    
    }
    
    

    Where are wrong?How can I fix it?
    Any suggestion will be very appreciated.



  • @Season02 I believe your chances of receiving a response will probably be better if you type in English



  • @Jakob Thank you,I will try later.



  • This post is deleted!


  • I still need study a lot about SIGNAL/SLOT.



  • SIGNAL和SLOT在Qt中是非常重要的内容。
    反映到Qt Quick,则是signal和signal handler。


Log in to reply
 

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