How to add a slot to a QWidget?

筅森魡賤 提交于 2019-12-12 00:45:08

问题


I have a QMainWindow, which has a QAction whose signal triggered() is connected to a slot 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. I can close it when I click the cancelbtn, but I can't enter the slot make_a_box().

Where can I add this slot in order to make this code working?


回答1:


This is ok and runs fine, because the slot you are using is located at the right place : in your occQt class.

// 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. 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 :

// 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 :

private slots : 
    void make_a_box();

And in your .cpp file :

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


来源:https://stackoverflow.com/questions/37385409/how-to-add-a-slot-to-a-qwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!