QT:没有这样的插槽 - 新手在这里

时间:2015-10-09 12:34:10

标签: c++ qt c++11 qt4

#include "mainwindow.h"
#include <QApplication>
#include "QProgressBar"
#include "QPushButton"
#include "QHBoxLayout"
#include "QLabel"
int main(int argc, char *argv[])
{

QApplication a(argc, argv);
QWidget *mainw = new QWidget;
mainw->setWindowTitle("Progress Increase");
QHBoxLayout *layout = new QHBoxLayout();
QPushButton *button = new QPushButton("PRESS TO INCREASE");
QLabel *newlabel = new QLabel();
newlabel->setText("WILL CHANGE HERE");
QObject::connect(button,SIGNAL(pressed()),newlabel,SLOT(setText("GAMA     GAMA")));
layout->addWidget(button);
layout->addWidget(newlabel);

mainw->setLayout(layout);
mainw->show();
return a.exec(); 
}

QT发出以下错误。我是新手 QObject :: connect:.. \ Gaama \ main.cpp中没有这样的插槽QLabel :: setText(&#34; GAMA GAMA&#34;):18

2 个答案:

答案 0 :(得分:0)

我建议您查看QObject::connect上的文档.Qt有一个非常好的文档,易于浏览和理解。

你得到那个错误,因为QObject :: connect的参数是   - 发射信号的物体   - 发出的信号   - 接收对象   - 和接收方法,称为插槽 并且您不能将参数值传递给信号,并且只传递它们所采用的类型。此外,插槽所采用的参数类型的数量必须等于或小于信号的参数类型。

所以例如你的代码

QObject::connect(button,SIGNAL(pressed()),newlabel,SLOT(setText("GAMA     GAMA")));

应该是

QObject::connect(button,SIGNAL(pressed()),newlabel,SLOT(setText()));

但这又错了,因为没有方法QLable :: setext() 我想你想要的是什么

QObject::connect(button,SIGNAL(pressed()),newlabel,SLOT(someSlot()));

然后定义someslot()并在其中将标签的文本设置为您想要的内容。

答案 1 :(得分:0)

  

的QObject ::连接(按钮,SIGNAL(按压()),newlabel,SLOT(的setText(“GAMA   GAMA“)));

如果要为QPushButton::pressed()事件创建处理程序,即在按下按钮后应更改标签文本,您可以

// Create a halper that transforms pressed() signal to textChanged(text) signal
// that will be connected to setText() slot, i.e.
// button.pressed() -> proxy.onButtonPressed() -> label.setText(text)
//
class SignalHelper : public QObject {
    Q_OBJECT
    QString  text_;
public:
    SignalHelper(const QString & text)
            : text_(text) {
    }
signals:
    void textChanged(const QString & text);
public slots:
    void onButtonPressed() {
        emit textChanged(text_);
    }
};

int main(int argc, char *argv[]) {
...
// Connect
//
QScopedPointer<SignalHelper> pHelper(new SignalHelper(QStringLiteral("GAMA     GAMA")));

QObject::connect(button, &QPushButton::pressed,
                 pHelper.data(), &SignalHelper::onButtonPressed);

QObject::connect(pHelper.data(), &SignalHelper::textChanged,
                 newlabel, &QLabel::setText);
...
// Also you can add another button that sets another text
//
QPushButton *button2 = new QPushButton("PRESS TO DECREASE");

QScopedPointer<SignalHelper> pHelper2(new SignalHelper(QStringLiteral("Decreased")));

QObject::connect(button2, &QPushButton::pressed,
                 pHelper2.data(), &SignalHelper::onButtonPressed);

QObject::connect(pHelper2.data(), &SignalHelper::textChanged,
                 newlabel, &QLabel::setText);

...
}

另一种方法是使用信号处理程序+ std :: bind() - 在这种情况下,您不需要在辅助类中定义任何特定信号,因此您可以将任何处理程序附加到pressed()信号。

typedef std::function<void()> Handler_t;

class BtnPressHandler : public QObject {
    Q_OBJECT
    Handler_t  handler_;
public:
    BtnPressHandler(Handler_t handler)
            : handler_(handler) {
    }
public slots:
    void onButtonPressed() {
        handler_();
    }
};

// Connect
//
QScopedPointer<BtnPressHandler> pHandler(new BtnPressHandler(
        std::bind(&QLabel::setText, newlabel, QStringLiteral("GAMA     GAMA"))));

QObject::connect(button, &QPushButton::pressed,
                 pHandler.data(), &BtnPressHandler::onButtonPressed);

// Create a button that closes an application
//
QPushButton * exitBtn = new QPushButton("PRESS TO EXIT");

QScopedPointer<BtnPressHandler> pHandler2(new BtnPressHandler(
        std::bind(&QApplication::quit, &a)));

QObject::connect(exitBtn, &QPushButton::pressed,
                 pHandler2.data(), &BtnPressHandler::onButtonPressed);