Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Chinese
  4. [SOLVED]In detial how to using SIGNAL/SOLT to interaction with UI widget in other CPP file?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Chinese
6 Posts 3 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Season02
    wrote on 1 Jun 2015, 10:04 last edited by Season02 6 Apr 2015, 05:06
    #1

    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.

    J 1 Reply Last reply 1 Jun 2015, 12:34
    0
    • S Season02
      1 Jun 2015, 10:04

      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.

      J Offline
      J Offline
      Jakob
      wrote on 1 Jun 2015, 12:34 last edited by
      #2

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

      S 2 Replies Last reply 1 Jun 2015, 12:41
      0
      • J Jakob
        1 Jun 2015, 12:34

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

        S Offline
        S Offline
        Season02
        wrote on 1 Jun 2015, 12:41 last edited by Season02 6 Jan 2015, 12:41
        #3

        @Jakob Thank you,I will try later.

        1 Reply Last reply
        0
        • J Jakob
          1 Jun 2015, 12:34

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

          S Offline
          S Offline
          Season02
          wrote on 1 Jun 2015, 13:00 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • S Offline
            S Offline
            Season02
            wrote on 4 Jun 2015, 05:05 last edited by
            #5

            I still need study a lot about SIGNAL/SLOT.

            1 Reply Last reply
            0
            • jiancaiyangJ Offline
              jiancaiyangJ Offline
              jiancaiyang
              wrote on 5 Jun 2015, 03:40 last edited by
              #6

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

              我们自己的论坛:http://qtdream.com
              擅长三维角色仿真动画。

              1 Reply Last reply
              0

              1/6

              1 Jun 2015, 10:04

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved