无法执行应在Qt中更改GUI的插槽

时间:2016-04-17 11:24:09

标签: c++ qt user-interface

我正在尝试实现信号/插槽连接,这将在editLine中显示警报。我不知道为什么插槽没有执行。

我试过了:

connect(mKlient, SIGNAL(showInfo(QString)), qApp, SLOT(aboutQt()));

确保信号正常工作。所以我认为插槽有问题。以下是代码:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <klient.h>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    Klient *mKlient;

private:
    Ui::MainWindow *ui;

public slots:
    void onShowInfo(QString);

private slots:
    //void on_pushButton_clicked();
    void on_pushButtonConnect_clicked();

};

#endif // MAINWINDOW_H

mainwindow.cpp

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


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

        onShowInfo("This alert is working");    // works

     //-------------------Here is the signal/slot connection-------------------------------------

        mKlient = new Klient(this);

         connect(mKlient,SIGNAL(showInfo(QString)),this, SLOT(onShowInfo(QString))); //not working - no idea why :/
      }

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

void MainWindow::onShowInfo(QString text){
    ui->lineEdit->setText(text);
}


void MainWindow::on_pushButtonConnect_clicked()
{
    if(ui->radioButtonTCP->isChecked()){
        mKlient->disconnect(this);
        mKlient->connectToServer(ui->lineEditIpAddress->text(),  ui->lineEditTcpPort->text().toInt());
    }
}

klient.h

#ifndef KLIENT_H
#define KLIENT_H

#include <QObject>
#include <QTcpSocket>
#include <QAbstractSocket>

class Klient : public QObject
{
    Q_OBJECT
public:
    explicit Klient(QObject *parent = 0);

    void connectToServer(QString,int);
    bool isConnectedFlag;

signals:
    void showInfo(QString);

public slots:
    void connected();

private:
    QTcpSocket *mSocket;

};

#endif // KLIENT_H

klient.cpp

#include "klient.h"

Klient::Klient(QObject *parent) :
    QObject(parent)
{
    isConnectedFlag = false;
}

void Klient::connectToServer(QString address, int port){


    mSocket = new QTcpSocket(this);
    connect(mSocket, SIGNAL(connected()),this,SLOT(connected()));

    mSocket->connectToHost(address,port);

    if(!mSocket->waitForConnected(1000)){
        //error mSocket->errorString();
    }

}

void Klient::connected(){

    emit showInfo("Connected");  //this information should be on lineEdit

    mSocket->write("siemanko");  //executing properly
    isConnectedFlag = true;
}

}

我正在按照本教程查找错误,但没有结果:

https://samdutton.wordpress.com/2008/10/03/debugging-signals-and-slots-in-qt/

我做错了吗?我是Qt的新手,所以访问GUI可能比我想象的要复杂得多。

1 个答案:

答案 0 :(得分:0)

我找到了答案......

void MainWindow::on_pushButtonConnect_clicked()
{
    if(ui->radioButtonTCP->isChecked()){
        mKlient->disconnect(this);
        mKlient->connectToServer(ui->lineEditIpAddress->text(),  ui->lineEditTcpPort->text().toInt());
    }
}

我以为我正在断开TCP连接。不幸的是,我正在断开信号/插槽连接。

我应该删除我的问题吗?怎么做?