看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Qt5.3.2 問題(Question): https://github.com/redkite1/qtelnetperso 下載了別人的telnet src "qtelnetperso.h" 主要做一個很簡單測試 我用一個button讓他按下後作連線 之後檢查連上了沒 連上了之後才開始做其他事 但測試過後好像在同一個button事件內永遠都是未連線 一直到事件結束才會變已連線 看code感覺是signal跟slot機制的關係? t是telnet的object button click只做以下兩件事 1. t->connectToHost 連上後會去更新connected這個member 2. t->isConnected() 永遠都是false 1跟2之間即使加了delay結果還是一樣 一直到button click的slot結束後 connected才會變true 請問應該如何做才能達到我想要的目的? 下面的code我只貼我自己測試的部分 src的部分可以參考上面的網址 程式碼(Code):(請善用置底文網頁, 記得排版) maindows.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include "qtelnetperso.h" #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_clicked(); void show_on_plaintext(QString text); void socketConnected(); void on_pushButton_2_clicked(); private: Ui::MainWindow *ui; QTelnetPerso *t; }; #endif // MAINWINDOW_H ***************************************************************** mainwindos.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include "QDebug" #include "QThread" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); t = new QTelnetPerso; connect(t,SIGNAL(message(QString)) , this , SLOT(show_on_plaintext(QString))); connect(t,SIGNAL(sockConnected()), this , SLOT(socketConnected())); } void MainWindow::show_on_plaintext(QString text) { ui->plainTextEdit->appendPlainText(text); } void MainWindow::socketConnected() { ui->plainTextEdit->appendPlainText("Connected"); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { t->connectToHost("ptt.cc" , 23); if(t->isConnected()) ui->plainTextEdit->appendPlainText("Connected"); return; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.228.234.175 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1431025445.A.E8C.html ※ 編輯: Mensholaten (36.228.234.175), 05/08/2015 03:10:03