[SOLVED]In detial how to using SIGNAL/SOLT to interaction with UI widget in other CPP file?
-
wrote on 1 Jun 2015, 10:04 last edited by Season02 6 Apr 2015, 05:06
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. -
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
-
@Season02 I believe your chances of receiving a response will probably be better if you type in English
wrote on 1 Jun 2015, 13:00 last edited byThis post is deleted! -
wrote on 4 Jun 2015, 05:05 last edited by
I still need study a lot about SIGNAL/SLOT.
-
wrote on 5 Jun 2015, 03:40 last edited by
SIGNAL和SLOT在Qt中是非常重要的内容。
反映到Qt Quick,则是signal和signal handler。
1/6