如何将按钮作为信号和表作为Qt中的插槽?

时间:2022-05-06 00:14:25

I know the very basic of Qt. The only thing I need is a button and a table. When the user presses the button, I fill the table in with data which comes from database. I have already figured out the database part, mostly.

我知道Qt的基本原理,我只需要一个按钮和一个表格。当用户按下按钮时,我用来自数据库的数据填充表格。大多数情况下,我已经算出了数据库部分。


Now my main problem is Qt signals and slots. In Java, we do it by events and handlers. How can I connect my button to an update of the table. Or if it is better, I could update the table when programs run.

现在我的主要问题是Qt信号和插槽。在Java中,我们通过事件和处理程序来完成。如何将按钮连接到表的更新。或者如果它更好,我可以在程序运行时更新表。

Any sample code or reference where I can find the right direction would be appreciated!

任何我能找到正确方向的样本代码或参考资料都将被欣赏!

1 个解决方案

#1


2  

This is very ismple. You would use Qt signal slot mechanism for this. You need to connect the button's clicked signal to your update handler slot for the database.

这是非常ismple。你会用Qt信号槽机制来做这个。您需要将按钮的单击信号连接到数据库的更新处理程序槽。

#include <QObject>
#include <QPushButton>

...

class UpdateHandler : public QObject
{
    Q_OBJECT
    public:
    explicit UpdateHandler(QObject *parent) : QObject(parent)
    {
        connect(&m_pushButton, SIGNAL(clicked(bool)), SLOT(updateDatabase()));
    }

    public slots:
    void updateDatabase()
    {
        // update the database here.
    }
    private:
        QPushButton m_pushButton;
};

With C++11 support, this would be even simpler as you could use lambda.

有了c++ 11支持,这将更加简单,因为您可以使用lambda。

    UpdateHandler::UpdateHandler(QObject *parent) : QObject(parent)
    {
        connect(&m_pushButton , &QPushButton::clicked,  [&] {
            // update the database here
        }};
    }

Disclaimer: this is just proto type code, so this was not even compilation-tested, but it should get you going, I think.

免责声明:这只是proto类型的代码,所以这甚至不是编译测试,但是我认为它应该会让你走。

Here is a good example how to create signals and slots using QtCreator:

这里有一个很好的例子,如何使用QtCreator创建信号和插槽:

Creating a Qt Widget Based Application

创建基于Qt小部件的应用程序。

如何将按钮作为信号和表作为Qt中的插槽?

Here you can find the all Qt 4.8 documentation for that if you still happen to use that:

在这里您可以找到所有Qt 4.8文档,如果您仍然使用它:

Qt Designer's Signals and Slots Editing Mode

Qt设计器的信号和插槽编辑模式。

如何将按钮作为信号和表作为Qt中的插槽?

#1


2  

This is very ismple. You would use Qt signal slot mechanism for this. You need to connect the button's clicked signal to your update handler slot for the database.

这是非常ismple。你会用Qt信号槽机制来做这个。您需要将按钮的单击信号连接到数据库的更新处理程序槽。

#include <QObject>
#include <QPushButton>

...

class UpdateHandler : public QObject
{
    Q_OBJECT
    public:
    explicit UpdateHandler(QObject *parent) : QObject(parent)
    {
        connect(&m_pushButton, SIGNAL(clicked(bool)), SLOT(updateDatabase()));
    }

    public slots:
    void updateDatabase()
    {
        // update the database here.
    }
    private:
        QPushButton m_pushButton;
};

With C++11 support, this would be even simpler as you could use lambda.

有了c++ 11支持,这将更加简单,因为您可以使用lambda。

    UpdateHandler::UpdateHandler(QObject *parent) : QObject(parent)
    {
        connect(&m_pushButton , &QPushButton::clicked,  [&] {
            // update the database here
        }};
    }

Disclaimer: this is just proto type code, so this was not even compilation-tested, but it should get you going, I think.

免责声明:这只是proto类型的代码,所以这甚至不是编译测试,但是我认为它应该会让你走。

Here is a good example how to create signals and slots using QtCreator:

这里有一个很好的例子,如何使用QtCreator创建信号和插槽:

Creating a Qt Widget Based Application

创建基于Qt小部件的应用程序。

如何将按钮作为信号和表作为Qt中的插槽?

Here you can find the all Qt 4.8 documentation for that if you still happen to use that:

在这里您可以找到所有Qt 4.8文档,如果您仍然使用它:

Qt Designer's Signals and Slots Editing Mode

Qt设计器的信号和插槽编辑模式。

如何将按钮作为信号和表作为Qt中的插槽?