如何添加一个插槽到QWidget? [英] How to add a slot to a QWidget?
问题描述
我有一个QMainWindow,它有一个 QAction
的信号 triggered()
连接到一个插槽 about2()
。
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);
}
当我运行 about2 code>,UI打开。我可以在点击
cancelbtn
时关闭它,但我无法输入 make_a_box()
。
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()
.
我可以在哪里添加此广告位,以使此代码正常运行?
推荐答案
这是确定和运行良好,因为您使用的插槽位于正确的位置:在 occQt
类。 / p>
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();
现在,这不行:
// 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()));
插槽 make_a_box()
存在 pWidget
,这是一个 QWidget
。您尝试将信号连接到不存在的插槽。
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.
您必须在 occQt $ c $中定义此插槽c> class ,并将按钮的信号
clicked()
连接到您课程中的插槽:
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()));
在您的.h文件中,您将有:
In your .h file, you will have :
private slots :
void make_a_box();
在您的.cpp文件中:
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);
}
这篇关于如何添加一个插槽到QWidget?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!