0
我採取以下鏈接與GUI:Qt的 - 基於GUI和線程多客戶端Socket編程
http://www.bogotobogo.com/Qt/Qt5_QTcpServer_Multithreaded_Client_Server.php
我的代碼上面的鏈接相同。
我知道這是不可能的:
class MainWindow : public QMainWindow , public QTcpServer
所以我剛剛創建:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "myserver.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(MyServer *ms, QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
和
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(MyServer *ms, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ms->StartServer();
connect(this,SIGNAL(SendCommand(QString)),ms,SLOT(GetCommand(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
和修改
//main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "myserver.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyServer *Server = new MyServer;
MainWindow w(Server);
w.show();
return a.exec();
}
正如我所說,我有一個服務器和多個線程處理客戶端。我想以某種方式將我的GUI連接到MyThread中的套接字。 我需要的全部是當我在文本框中編寫一些文本並按下mainwindow中的按鈕時,它會向套接字發送請求並將響應返回給另一個文本框。
我已經接受了,但我不明白。
謝謝親愛的亞歷克斯, MyServer類中的MyThread類不在MainWindow中,因此我無法連接它們。如果你知道我真的很感謝你給我一個詳細的例子 –
好吧,請一個問題。是否有必要在主函數中創建MyServer對象,或者可以將它移動到MainWindow類?因爲將它移動到'MainWindow'將簡化你的構造函數,並且簡化了一些解決你的問題的方法,我想。 – Alex
沒問題。最好爲我移動 –