我这个小辣鸡又回来了,抱歉各位,我发现自己好low啊,指针的传值真正的内涵都没搞懂,有点丢人,哎,今天算是明白了,不亏血赚。
背景:还是接着上一个项目优化的,我把问题简化了一下,就是怎么关联两个控件,就比如两个按钮是相互影响的,是接着上篇sender函数的操作,从一百个按钮中拿到一个btn1,点击它,他后边那个按钮btn2,会和他一起消失,点击btn2,btn1会发生其他动作。
解决:我就简化成两个按钮,跟大家分享一下。
重新定义继承QPushButton的类
class MyPushButton : public QPushButton
{
Q_OBJECT
public:
QPushButton *m_pBtnModify;
};
.cpp文件:
myPushButton1 = new MyPushButton;
myPushButton2 = new MyPushButton;
myPushButton1->setFixedSize(20,20);
myPushButton1->setText("+");
myPushButton2->setFixedSize(20,20);
myPushButton2->setText("+");
QGridLayout *grid = new QGridLayout(this);
grid->setColumnMinimumWidth(0, 240);
grid->addWidget(myPushButton1,0,0);
grid->addWidget(myPushButton2,0,1);
myPushButton1->m_pBtnModify = myPushButton2;
myPushButton2->m_pBtnModify = myPushButton1;
connect(myPushButton1, SIGNAL(clicked()), this, SLOT(slot1()));
connect(myPushButton2, SIGNAL(clicked()), this, SLOT(slot2()));
槽函数:
void Dialog::slot1() //在无法拿到btn2,只能拿到btn1的情况下,还要对btn2进行操作
{
delete myPushButton1;
delete myPushButton1->m_pBtnModify;
}