Qt c ++ GUI调用从另一个类 [英] Qt c++ GUI call from another class

查看:213
本文介绍了Qt c ++ GUI调用从另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过gui drag& drop创建了一个按钮和一个文本浏览器。
在mainwindow.cpp中创建ui以及点击按钮功能。有一个main.cpp但是这是不相关的原因程序不会启动,直到启动按钮被点击。

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;
}

到目前为止,这一切都很好,问题出在myServer.cpp我想通过 ui-> textBrowser-> append(hello hello); 在textBrowser中写一些东西。但是myServer.cpp类不知道ui。 ui未声明标识符

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我会创建一个新的b $ b MainWindow test; 并通过 test.function();
调用函数不工作吗?

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

推荐答案

首先,当您在MainWindow :: on_StartButtonClicked函数中创建MyServer对象时,要动态创建,否则它将超出范围并被删除,但也许你只是显示这个,而不是它在MainWindow标题中的声明。

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.

对于您的问题,您的UI已连接到MainWindow,因此使用Qt的信号和插槽将来自MyServer对象的信号连接到MainWindow并发送要显示的文本。然后MainWindow可以将其添加到textBrowser。类似这样: -

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)));
}

然后代替调用ui-> textBrowser-> append(hallo hallo );在newConnection中,发出信号: -

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

emit updateUI("hallo hallo");

在MainWindow中,您将拥有AppendToBrowser函数: -

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

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

或者,您可以将UI对象的指针传递给MyServer,它从那里,但是信号和slot方法是更干净。

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标头

//Skeleton My Server header

class MyServer : public QObject
{
    QOBJECT

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

// Skeleton MainWindow标题

// Skeleton MainWindow header

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

这篇关于Qt c ++ GUI调用从另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆