c++ 按下按钮时如何运行命令?

wko9yo5t  于 2023-05-30  发布在  其他
关注(0)|答案(5)|浏览(185)

我正在尝试构建一个Qt应用程序,作为Qt Creator提供的示例。
代码:
arrowpad.h

#ifndef ARROWPAD_H
#define ARROWPAD_H

#include <QWidget>

QT_BEGIN_NAMESPACE
class QPushButton;
QT_END_NAMESPACE

//! [0]
class ArrowPad : public QWidget
//! [0] //! [1]
{
//! [1] //! [2]
    Q_OBJECT
//! [2]

public:
    ArrowPad(QWidget *parent = 0);

private:
    QPushButton *upButton;
    QPushButton *downButton;
    QPushButton *leftButton;
    QPushButton *rightButton;
};

#endif

arrowpad.cpp

#include <QtGui>

#include "arrowpad.h"

ArrowPad::ArrowPad(QWidget *parent)
    : QWidget(parent)
{
//! [0]
    upButton = new QPushButton(tr("&Up"));
//! [0] //! [1]
    downButton = new QPushButton(tr("&Down"));
//! [1] //! [2]
    leftButton = new QPushButton(tr("&Left"));
//! [2] //! [3]
    rightButton = new QPushButton(tr("&Right"));
//! [3]

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(upButton, 0, 1);
    mainLayout->addWidget(leftButton, 1, 0);
    mainLayout->addWidget(rightButton, 1, 2);
    mainLayout->addWidget(downButton, 2, 1);
    setLayout(mainLayout);
}

当我按下一个按钮时,如何让它运行一个命令?
我发现了这个:How to run a system command in Qt?
但我不知道如何使用它与QPushButton,它只是认为这是一个字符串的文本。
任何命令都必须在Linux Fedora 19中工作,因为这是我运行它的地方。
下面是当前的结果,顺便说一下,我不认为在上面的.cpp脚本中按钮名称被指定为(tr("&Up"));

5w9g7ksd

5w9g7ksd1#

Aparidge是正确的,但要想增加这一点,并有多个按钮连接到相同的功能,你只需要连接正确的信号和插槽。
例如:

connect(upButton, SIGNAL(clicked()), this, SLOT(onButtonPressed()));
connect(downButton, SIGNAL(clicked()), this, SLOT(onButtonPressed()));
connect(leftButton, SIGNAL(clicked()), this, SLOT(onSomeOtherButtonPressed()));
connect(rightButton, SIGNAL(clicked()), this, SLOT(onYetAnotherButtonPressed()));

将其放入构造函数中会将upButton和downButton连接到同一个插槽onButtonPressed()。leftButton将触发onSomeOtherButtonPressed(),rightButton将触发onYetAnotherButtonPressed()。

c9qzyr3d

c9qzyr3d2#

单击该按钮时,它会发出pressed信号。您必须做的是将此信号连接到插槽,然后在该插槽中执行操作。
比如说

ArrowPad::ArrowPad(QWidget *parent)
    : QWidget(parent)
{

    upButton = new QPushButton(tr("&Up"));
    connect(upButton, SIGNAL(clicked()), this, SLOT(onButtonPressed()));
    // And so on for the other buttons
}

void ArrowPad::onButtonPressed()
{
    // Button is pressed, do actions
}
ncgqoxb0

ncgqoxb03#

在qt中,大多数事情都是通过信号/插槽机制来完成的。因此,您可以定义一些插槽,并简单地将信号连接到它们。
arrowpad.h

#ifndef ARROWPAD_H
#define ARROWPAD_H

#include <QWidget>

QT_BEGIN_NAMESPACE
class QPushButton;
QT_END_NAMESPACE

//! [0]
class ArrowPad : public QWidget
//! [0] //! [1]
{
//! [1] //! [2]
    Q_OBJECT
//! [2]

public:
    ArrowPad(QWidget *parent = 0);

private:
    QPushButton *upButton;
    QPushButton *downButton;
    QPushButton *leftButton;
    QPushButton *rightButton;


// 1- Here you declare the slots
private slots:
 void onUpButtonClick();
 void onDownButtonClick();
 void onLeftButtonClick();
 void onRightButtonClick();
};

#endif

现在在arrowpad.cpp中

#include <QtGui>

#include "arrowpad.h"

ArrowPad::ArrowPad(QWidget *parent)
    : QWidget(parent)
{
//! [0]
    upButton = new QPushButton(tr("&Up"));
//! [0] //! [1]
    downButton = new QPushButton(tr("&Down"));
//! [1] //! [2]
    leftButton = new QPushButton(tr("&Left"));
//! [2] //! [3]
    rightButton = new QPushButton(tr("&Right"));
//! [3]

// 2- Connect signals to slots
  QObject::connect(upButton, signal(clicked()), this, slot(onUpButtonClick()));

  QObject::connect(downButton, signal(clicked()), this, slot(onDownButtonClick()));

  QObject::connect(rightButton, signal(clicked()), this, slot(onRightButtonClick()));

  QObject::connect(leftButton, signal(clicked()), this, slot(onLeftButtonClick()));

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(upButton, 0, 1);
    mainLayout->addWidget(leftButton, 1, 0);
    mainLayout->addWidget(rightButton, 1, 2);
    mainLayout->addWidget(downButton, 2, 1);
    setLayout(mainLayout);
}

// 3- Define the slots
 void ArrowPad::onUpButtonClick()
{
   qDebug() << "onUpButtonClick clicked";
}
 void ArrowPad::onDownButtonClick()
{
   qDebug() << "onDownButtonClick clicked";
}
 void ArrowPad::onLeftButtonClick()
{
   qDebug() << "onLeftButtonClick clicked";
}
 void ArrowPad::onRightButtonClick()
{
   qDebug() << "onRightButtonClick clicked";
}
7z5jn7bk

7z5jn7bk5#

以下是如何使用lambda合并QProcessQPushButton信号的示例:

QPushButton *button = new QPushButton();
QProcess *process = new QProcess();

//connect QPushButton pressed signals to a lambda
button->connect(button,&QPushButton::pressed,[=]()
{
    //Here you can specify what you need to happen when the button is pressed
    process->startCommand("Some command");
});

button->show();

阅读有关Signals & SlotsQProcess的更多信息,以便能够调整此示例,并阅读此内容以获取更多示例:Qt Slots and C++11 lambda

相关问题