首页 > c++ > 我可以在不创建课程的情况下创建插槽吗?

我可以在不创建课程的情况下创建插槽吗? (Can I create a slot without creating a class?)

2019-03-11 c++qt

问题

只是一个简单的编程来说明更大的问题。我想要做的是创建一个信号/插槽连接功能,而不使用任何类。我是OOP的新手,现在不想和班级有关。所以,我在函数之前创建了一个函数,main并希望这可以工作......但事实并非如此。可以编译和执行编程,但我收到通知:Object::connect: No such slot QTextEdit::onClicked()。我预计这个问题可以很容易地解决,因为这是一个表达错误,而不是理解,所以提前感谢你的帮助!:)

#include <QtGui>
#include <QtCore>

    void onClicked (QTextEdit text, QString a)
    {
        text.setText(a);
    }

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);

        QWidget mw;
        mw.setWindowTitle("Main Window");
        mw.resize(400, 400);
        mw.show();

        QLabel label ("Enter something:", &mw);
        label.setAlignment(Qt::AlignHCenter);
        label.show();

        QLineEdit line (&mw);
        line.show();

        QString a = line.text();

        QTextEdit text (&mw);
        text.show();

        QPushButton btn ("Convert", &mw);
        QObject::connect(
        &btn,
        SIGNAL(clicked()),
        &text,
        SLOT(onClicked()));
        btn.show();

        QVBoxLayout layout_mw;

        layout_mw.addWidget(&label);
        layout_mw.addWidget(&line);
        layout_mw.addWidget(&btn);
        layout_mw.addWidget(&text);

        mw.setLayout(&layout_mw);

        return app.exec();
    }

解决方法

您可以在Qt文档中阅读:

从QObject或其子类之一继承的所有类(例如, QWidget)可以包含信号和插槽。

所以你不能在main.cpp中定义槽。你有两个选择。定义从QObject派生的类并在那里添加插槽或使用Qt 5. *并使用新的连接语法将信号连接到lambda。

问题

Just a simple prog to illustrate the issue in a larger one. What I am trying to do is to create a function for signal/slot connection without using any classes. I am new to OOP and don't want to have to do with classes at the moment. So, I am creating a function before the main function and hope this will work... but it doesn't. The prog can be compiled and executed, but I get a notification: Object::connect: No such slot QTextEdit::onClicked(). I anticipate the issue can very easily be solved as it is rather a mistake of expression than of understanding, so thanks in advance for your help! :)

#include <QtGui>
#include <QtCore>

    void onClicked (QTextEdit text, QString a)
    {
        text.setText(a);
    }

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);

        QWidget mw;
        mw.setWindowTitle("Main Window");
        mw.resize(400, 400);
        mw.show();

        QLabel label ("Enter something:", &mw);
        label.setAlignment(Qt::AlignHCenter);
        label.show();

        QLineEdit line (&mw);
        line.show();

        QString a = line.text();

        QTextEdit text (&mw);
        text.show();

        QPushButton btn ("Convert", &mw);
        QObject::connect(
        &btn,
        SIGNAL(clicked()),
        &text,
        SLOT(onClicked()));
        btn.show();

        QVBoxLayout layout_mw;

        layout_mw.addWidget(&label);
        layout_mw.addWidget(&line);
        layout_mw.addWidget(&btn);
        layout_mw.addWidget(&text);

        mw.setLayout(&layout_mw);

        return app.exec();
    }

解决方法

You can read in the Qt documentation that:

All classes that inherit from QObject or one of its subclasses (e.g., QWidget) can contain signals and slots.

So you can not define slots in main.cpp. You have two options. Either define a class derived from QObject and add your slot there or use Qt 5.* and connect your signal to a lambda using the new syntax for connections.

相似信息