如何从按下Qt-C ++的按钮运行命令

时间:2013-12-12 17:53:19

标签: c++ qt command-line qt-creator

当我试图完成一个程序,并且我想要一个图形界面时,我正在尝试非常快速地重建QT Creator作为示例提供的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;
};

#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);
}

按下按钮时如何让它运行命令?我找到了this,但我不知道如何整合qpushbutton的东西,其他它只是认为它是一串文字...... 任何命令都必须在LINUX中工作 - FEDORA 19,as这就是我正在运行它。

以下是当前结果 - 顺便说一下,我认为按钮名称在上面的(tr("&Up"));脚本中被指定为.cpp

qt app

4 个答案:

答案 0 :(得分:3)

Apartidge是正确的,但要添加到该功能并将多个按钮连接到同一功能,您只需连接正确的信号和插槽即可。

对于你的例子:

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()。

答案 1 :(得分:2)

单击该按钮时会发出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
}

答案 2 :(得分:1)

您是否尝试过了解SIGNAL和SLOT机制?

http://doc.qt.io/qt-4.8/signalsandslots.html

答案 3 :(得分:1)

在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";
}