簡體   English   中英

QT 使用 SIGNAL 和 SLOT 更改 GUI

[英]QT change GUI with SIGNAL and SLOT

我有一個關於開發 QT 應用程序的問題。 我想使用 SINGAL 和 SLOT 更改 GUI。 sendValue() 從 mythread.cpp 到 showValue() 從 myserver.cpp 工作良好。 那連接得很好,我可以檢查值。

但是從 myserver.cpp 到 showApp() 從 application.cpp 的 sendApp() 不起作用。 我很困惑沒有連接或沒有發射或其他問題。 有兩個問題。 一種是如何使用 SIGNAL 和 SLOT 從 sendApp() 連接到 showApp()。 發射不工作? 或連接不起作用? 我找不到原因。 另一個是如果 showApp() 函數可以工作嗎? 請回答我。 很抱歉這是我第一次開發 QT 應用程序。

應用程序.h

#ifndef APPLICATION_H
#define APPLICATION_H

#include <QWidget>
#include <QObject>
#include <menu.h>
#include <QTcpServer>
#include "myserver.h"
#include "mythread.h"

QT_BEGIN_NAMESPACE
namespace Ui { class application; }
QT_END_NAMESPACE

class application : public QWidget
{
    Q_OBJECT

public:
    application(QWidget *parent = nullptr);
    ~application();

private slots:
    void on_menuBt_clicked();
    void menuBackBtClicked();
    void showApp(QString strValue);

private:
    Ui::application *ui;
    menu menuPage;

};

#endif // APPLICATION_H

應用程序.cpp

#include "application.h"
#include "ui_application.h"
#include "myserver.h"
#include "mythread.h"

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

    Myserver *myserver = new Myserver(this);


    ui->firstToggle->setVisible(false);
    ui->secondToggle->setVisible(false);
    ui->thirdToggle->setVisible(false);
    ui->forthToggle->setVisible(false);
    ui->stackedWidget->addWidget(&menuPage);

    ui->stackedWidget->insertWidget(1, &menuPage);

    connect(&menuPage, SIGNAL(menuBackClick()), this, SLOT(menuBackBtClicked()));
    connect(myserver, SIGNAL(sendApp(QString)), this, SLOT(showApp(QString)));

    setWindowFlags(Qt::FramelessWindowHint);

}

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


void application::on_menuBt_clicked()
{
    ui->stackedWidget->setCurrentIndex(1); //menuLayout
}

void application::menuBackBtClicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void application::showApp(QString strValue) {
    QString compare = "13";
    int x = QString::compare(strValue,compare, Qt::CaseInsensitive);
    if(x == 0) {
        ui->firstToggle->setVisible(true);
    }
}

我的服務器.h

#ifndef MYSERVER_H
#define MYSERVER_H

#include <QObject>
#include <QTcpServer>
#include "mythread.h"
#include "application.h"

class Myserver : public QTcpServer
{
    Q_OBJECT

public:
    explicit Myserver(QObject *parent = 0);
    void startServer();
signals:
    void sendApp(QString strValue);

private slots:
    void showValue(QString strValue);

protected:
    void incomingConnection(qintptr socketDescriptor);

private:
    MyThread *mythread;


};

#endif // MYSERVER_H

我的服務器.cpp

#include "myserver.h"
#include "mythread.h"
#include "application.h"

Myserver::Myserver(QObject *parent) :
    QTcpServer(parent)
{
}

void Myserver::startServer()
{
    int port = 3333;

    if(!this->listen(QHostAddress::Any,port))
    {
        qDebug() << "Could not start server";
    }
    else
    {
        qDebug() << "Listening to port " << port << "...";
    }
}

void Myserver::incomingConnection(qintptr socketDescriptor)
{
    // We have a new connection
    qDebug() << socketDescriptor << " Connecting...";

    MyThread *thread = new MyThread(socketDescriptor, this);

    // connect signal/slot
    // once a thread is not needed, it will be beleted later
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    connect(thread, SIGNAL(sendValue(QString)), this, SLOT(showValue(QString)));

    thread->start();
}
void Myserver::showValue(QString strValue){
    emit sendApp(strValue);
}

我的線程.h

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QObject>
#include <QTcpSocket>
#include <QThread>
#include <QDebug>

class MyThread : public QThread
{
    Q_OBJECT
public:
    explicit MyThread(qintptr ID, QObject *parent = 0);
    void run();

signals:
    void error(QTcpSocket::SocketError socketerror);
    void sendValue(QString strValue);

private slots:
    void readyRead();
    void disconnected();

private:
    QTcpSocket *socket;
    qintptr socketDescriptor;

};

#endif // MYTHREAD_H

我的線程.cpp

#include "mythread.h"

#include <QThread>

MyThread::MyThread(qintptr ID, QObject *parent) :
    QThread(parent)
{
    this->socketDescriptor = ID;
}

int count = 0;

void MyThread::run()
{
    // thread starts here
    qDebug() << " Thread started";

    socket = new QTcpSocket();

    // set the ID
    if(!socket->setSocketDescriptor(this->socketDescriptor))
    {
        // something's wrong, we just emit a signal
        emit error(socket->error());
        return;
    }

    // connect socket and signal
    // note - Qt::DirectConnection is used because it's multithreaded
    //        This makes the slot to be invoked immediately, when the signal is emitted.

    connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
    connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));

    // We'll have multiple clients, we want to know which is which
    qDebug() << socketDescriptor << "Client connected";

    QString strValue = QString::number(socketDescriptor);

    emit sendValue(strValue);

    // make this thread a loop,
    // thread will stay alive so that signal/slot to function properly
    // not dropped out in the middle when thread dies

    exec();
}

void MyThread::readyRead()
{
    // get the information
    QByteArray Data = socket->readAll();

    // will write on server side window
    qDebug() << socketDescriptor << " Data in: " << Data;

    socket->write(Data);
}

void MyThread::disconnected()
{
    qDebug() << socketDescriptor << " Disconnected";

    socket->deleteLater();
    exit(0);
}

你只是忘記啟動你的服務器了!

它可能只是通過在應用程序類構造函數中的連接之后添加startServer()函數調用來工作。

connect(&menuPage, SIGNAL(menuBackClick()), this, SLOT(menuBackBtClicked()));
connect(myserver, SIGNAL(sendApp(QString)), this, SLOT(showApp(QString)));

myserver->startServer();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM