(Qt)从PushButton的QButtonGroup创建信号? [英] (Qt) Create signal from QButtonGroup of PushButtons?

查看:1650
本文介绍了(Qt)从PushButton的QButtonGroup创建信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一些按钮,我把这个放在一个组里:

  MainWindow :: MainWindow(QWidget * parent):
QMainWindow(parent),
ui(new Ui :: MainWindow)
{
ui-> setupUi(this);
AddSlotsToGroup();
}

void MainWindow :: AddSlotsToGroup()
{
QButtonGroup * group = new QButtonGroup(this);
group-> addButton(ui-> slot_0);
group-> addButton(ui-> slot_1);
// ...
}

获取在该组中点击的按钮的ID。 (对不起,如果我解释不好:()



这是我做的(纯粹的猜测后谷歌搜索)

  MainWindow :: MainWindow(QWidget * parent):
QMainWindow(parent),
ui(new Ui :: MainWindow)
{
ui-> setupUi(this);
AddSlotsToGroup();
connect(QPushButton * group,SIGNAL(buttonClicked(int)),this,SLOT(onGroupButtonClicked(int)));
}

void MainWindow :: onGroupButtonClicked(int id)
{
qDebug()<< id;
}

毫不奇怪,我收到一个错误说组是一个未声明的标识符,QPushButton是非法使用等。



我讨厌说我只使用设计器窗口中的信号/槽,所以我真的只需要这一个东西,然后我为未来设置:。)



感谢您的时间。 :)

解决方案

请尝试以下操作:

 code> MainWindow :: MainWindow(QWidget * parent):
QMainWindow(parent),
ui(new Ui :: MainWindow)
{
ui-> setupUi (这个);
AddSlotsToGroup();
}

void MainWindow :: AddSlotsToGroup()
{
QButtonGroup * group = new QButtonGroup(this);
group-> addButton(ui-> slot_0);
group-> addButton(ui-> slot_1);
// ...
connect(group,SIGNAL(buttonClicked(int)),
this,SLOT(onGroupButtonClicked(int)));
}

顺便说一句,你需要先学习C ++来掌握Qt。 p>

I am so confused on how this whole thing works.

I have some pushbuttons that I put into a group like this:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    AddSlotsToGroup();
}

void MainWindow::AddSlotsToGroup()
{
    QButtonGroup* group = new QButtonGroup(this);
    group->addButton(ui->slot_0);
    group->addButton(ui->slot_1);
    //...
}

And I want to create a slot that gets the id of the button that was clicked in that group. (Sorry if I explained that poorly :( )

So this is what I did (pure guess after googling)

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    AddSlotsToGroup();
    connect(QPushButton* group, SIGNAL(buttonClicked(int)), this, SLOT(onGroupButtonClicked(int)));
}

void MainWindow::onGroupButtonClicked(int id)
{
    qDebug() << id;
}

And to no surprise, I got an error saying group is an undeclared identifier and that QPushButton was an illegal use etc.

I hate to say that I have only used signals/slots from the designer window, so I really just need this one thing, and then I'm set for the future. :)

Thanks for your time. :)

解决方案

Try the following:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    AddSlotsToGroup();
}

void MainWindow::AddSlotsToGroup()
{
    QButtonGroup* group = new QButtonGroup(this);
    group->addButton(ui->slot_0);
    group->addButton(ui->slot_1);
    //...
    connect(group, SIGNAL(buttonClicked(int)),
            this, SLOT(onGroupButtonClicked(int)));
}

By the way, you need to learn C++ first to master Qt.

这篇关于(Qt)从PushButton的QButtonGroup创建信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆