Qt c++ GUI call from another class

二次信任 提交于 2019-12-07 19:03:14

问题


I created a button and a textbrowser via gui drag&drop. the ui is created in the mainwindow.cpp as well as the click-button-function. There is a main.cpp but thats irrelevant cause the program shall not start until the startbutton is clicked.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myserver.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_startButton_clicked()
{
    MyServer mServer;
}

This is all fine so far, the problem is in the myServer.cpp where I want to write something into the textBrowser via ui->textBrowser->append("hello hello");. but the myServer.cpp class doesn't "know" the ui. "ui" not declared identifier

#include "myserver.h"
#include "mainwindow.h"


MyServer::MyServer(QObject *parent) :
    QObject(parent)
{
}

void MyServer::newConnection()
{
    server = new QTcpServer(this);

    connect(server,SIGNAL(newConnection()),this,SLOT(newConnection()));

    int ports = MainWindow::port();
    if(!server->listen(QHostAddress::Any,ports))
    {

    }
    else
    {
        //here is the problem
        ui->textBrowser->append("hallo hallo");
    }
}

normaly i would create a new (for example) MainWindow test; and call functions via this test.function(); but this does not work here?


回答1:


First of all, when you create the MyServer object in the MainWindow::on_StartButtonClicked function, the object needs to be created dynamically, else it will go out of scope and get deleted, but perhaps you're just showing this, rather than its declaration in the MainWindow header.

As to your question, your UI is connected to the MainWindow, so use Qt's signals and slots to connect a signal from the MyServer object to the MainWindow and send it the text to be displayed. Then the MainWindow can add it to the textBrowser. Something like this: -

void MainWindow::on_startButton_clicked()
{
    MyServer* mServer = new MyServer;
    connect(mServer SIGNAL(updateUI(const QString)), this, SLOT(AppendToBrowser(const QString)));
}

Then instead of calling ui->textBrowser->append("hallo hallo"); in newConnection, emit the signal: -

emit updateUI("hallo hallo");

In MainWindow, you'd have the function AppendToBrowser: -

void MainWindow::AppendToBrowser(const QString text)
{
    ui->textBrowser->append(text);
}

Alternatively, you could pass a pointer of the UI object to the MyServer and call it from there, but the signals and slots method is much cleaner.

=========== Edited for headers, in response to comments ======================

//Skeleton My Server header

class MyServer : public QObject
{
    QOBJECT

    signals:
         void updateUI(const QString text);
};

// Skeleton MainWindow header

class MainWindow : public QMainWindow
{
    private slots:
        void AppendToBrowser(const QString text);
};



回答2:


You have 2 options:
1) code a signal into MyServer class that passes the data needed to update the gui and a slot into MainWindow class that does the work of updating the ui and connect the signal with the slot.

or 2) you can pass a pointer to MainWindow into the MyServer (maybe it makes sense to be the parent) and use that pointer to call a public functionality that you code into the MainWindow and updates the ui with the data you need.

LE: Two issues:
1) i see you create the MyServer instance on stack into the *on_startButton_clicked*, this might be an issue if that object gets destroyed that fast, so you should make sure that stays alive for as long as you need it to, so that it can do it's work.
2) what is this line supposed to do: connect(server,SIGNAL(newConnection()),this,SLOT(newConnection())); even if you have a newConnection signal why are you connecting that into the slot that you connect with and how that is supposed to connect the first time, to execute the slot and make the connection, so check what you have done there...



来源:https://stackoverflow.com/questions/17450039/qt-c-gui-call-from-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!