简体   繁体   English

如何在QWidget中添加插槽?

[英]How to add a slot to a QWidget?

I have a QMainWindow, which has a QAction whose signal triggered() is connected to a slot about2() . 我有一个的QMainWindow,其具有QAction其信号triggered()被连接到狭槽about2()

...
connect(mAboutAction2, SIGNAL(triggered()), this, SLOT(about2()));
...


void occQt::about2()  //UI
{
    QWidget* pWidget = new QWidget;
    QPushButton* okbtn = new QPushButton(tr("ok"));
    QPushButton* cancelbtn = new QPushButton(tr("cancel"));
    btnlayout->addWidget(okbtn);
    btnlayout->addWidget(cancelbtn);
    dlglayout->setMargin(50);
    dlglayout->addLayout(gridlayout);
    dlglayout->addStretch(40);
    dlglayout->addLayout(btnlayout);
    pWidget->setLayout(dlglayout);
    pWidget->setWindowTitle(tr("Make a Box by custom."));
    pWidget->show();
    connect(okbtn, SIGNAL(clicked()), pWidget, SLOT(make_a_box()));
    connect(cancelbtn, SIGNAL(clicked()), pWidget, SLOT(close()));
}

void occQt::make_a_box()
{
    TopoDS_Shape aTopoBox = BRepPrimAPI_MakeBox(3.0, 4.0, 95.0).Shape();
    Handle_AIS_Shape anAisBox = new AIS_Shape(aTopoBox);

    anAisBox->SetColor(Quantity_NOC_AZURE);

    mContext->Display(anAisBox);
}

When I run the slot about2() , the UI opens. 当我运行插槽about2() ,UI将打开。 I can close it when I click the cancelbtn , but I can't enter the slot make_a_box() . 单击cancelbtn时可以将其关闭,但无法输入插槽make_a_box()

Where can I add this slot in order to make this code working? 在哪里可以添加此插槽以使此代码正常工作?

This is ok and runs fine, because the slot you are using is located at the right place : in your occQt class. 没关系,可以正常运行,因为您使用的插槽位于occQt类中的正确位置:。

// You connect the signal FROM the action TO "this", i.e. your class
connect(mAboutAction2, SIGNAL(triggered()), this, SLOT(about2()));

void occQt::about2()  //UI
{

    QWidget* pWidget = new QWidget;
    QPushButton* okbtn = new QPushButton(tr("ok"));
    QPushButton* cancelbtn = new QPushButton(tr("cancel"));
    btnlayout->addWidget(okbtn);
    btnlayout->addWidget(cancelbtn);
    dlglayout->setMargin(50);
    dlglayout->addLayout(gridlayout);
    dlglayout->addStretch(40);
    dlglayout->addLayout(btnlayout);
    pWidget->setLayout(dlglayout);
    pWidget->setWindowTitle(tr("Make a Box by custom."));
    pWidget->show();

Now, this is not ok : 现在,这不行:

 // You connect the signal FROM the button to pWidget, which doesn't have a slot make_a_box()
 connect(okbtn, SIGNAL(clicked()), pWidget, SLOT(make_a_box()));

The slot make_a_box() doesn't exist for pWidget , which is a QWidget . make_a_box()不存在pWidget ,这是一个QWidget You are trying to connect a signal to a slot that does not exist. 您正在尝试将信号连接到不存在的插槽。

You have to define this slot in your occQt class, and connect the signal clicked() of the button to your slot in your class : 您必须在occQt类中定义此插槽, 然后将按钮的信号clicked()连接到类中的插槽

// Now, you connect the signal FROM the button to "this", which is your class and has a slot make_a_box()
connect(okbtn, SIGNAL(clicked()), this, SLOT(make_a_box()));

In your .h file, you will have : 在您的.h文件中,您将具有:

private slots : 
    void make_a_box();

And in your .cpp file : 在您的.cpp文件中:

void occQt::make_a_box()
{
    TopoDS_Shape aTopoBox = BRepPrimAPI_MakeBox(3.0, 4.0, 95.0).Shape();
    Handle_AIS_Shape anAisBox = new AIS_Shape(aTopoBox);

    anAisBox->SetColor(Quantity_NOC_AZURE);

    mContext->Display(anAisBox);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM