Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Learning
  3. Qt in Education
  4. [SOLVED]Where are wrong,I need operate UI widget in my.cpp
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Where are wrong,I need operate UI widget in my.cpp

Scheduled Pinned Locked Moved Qt in Education
3 Posts 2 Posters 1.5k 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.
  • Season02S Offline
    Season02S Offline
    Season02
    wrote on last edited by Season02
    #1

    I had add a listwidget in mainwindow.ui,Then i want to add item in testcalss.cpp by SIGNAL/SLOT,but i failed.My code are followed,where are wrong?Any help will very appreciated.

    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);
        //void getadd(const QString& text);
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    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;
        t.em();
    }
    
    void MainWindow::additem(const QString& text)
    {
        ui->listWidget->addItem("text");
        ui->listWidget->addItem(text);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    

    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();
    
        void em();
    
    signals:
        void add(const QString& text);
    
    public slots:
    
    private:
        //MainWindow *mainwindow;
    };
    
    #endif // TESTCLASS_H
    

    testclass.cpp

    #include "testclass.h"
    
    testclass::testclass(QObject *parent) : QObject(parent)
    {
        connect(this,SIGNAL(add(const QString&)),parent,SLOT(additem(const QString&)));
    }
    
    void testclass::em()
    {
        emit add("simulation");
    }
    
    testclass::~testclass()
    {
    
    }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      the problem is that you create the testsclass instance t inside the MainWindow constructor and when it goes out of the scope it is destroyed (also invalidating connections).
      Also you used the parent in the connect function but you never set it.

      You could define t as member of MainWindow or, better, in the main() function.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      Season02S 1 Reply Last reply
      1
      • M mcosta

        Hi,

        the problem is that you create the testsclass instance t inside the MainWindow constructor and when it goes out of the scope it is destroyed (also invalidating connections).
        Also you used the parent in the connect function but you never set it.

        You could define t as member of MainWindow or, better, in the main() function.

        Season02S Offline
        Season02S Offline
        Season02
        wrote on last edited by
        #3

        @mcosta Thank you.I have many mistake in my code,like what you said.

        1 Reply Last reply
        0

        • Login

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