QPushbutton 槽函数被多次触发

QPushbutton 槽函数被多次触发

修改前

 --------------TODO绑定函数--------------
     投屏/结束投屏 
    connect(&this->attachToDevice, &QPushButton::clicked, this, &PhoneCard::onAttachToDevice);
    connect(&this->detachFromDevice, &QPushButton::clicked, this, &PhoneCard::onDetachFromDevice);

每次都会绑定信号槽,但是不是重新绑定的,也就是变成多个信号槽了

所以 每次点击刷新按键之后 再点击 被刷新的按键 ,被刷新的按键都会多一个触发机制(暂且这么理解)

然后当你再次点击 被刷新的按钮之后 就会导致触发多次。对应的里面加的QMessageBox 就会出现多次

解决

void PhoneCard::drawButtons(int modType) {

    // to phone
    if(modType == 1){
        // 投到手机
        this->attachToDevice.setParent(this);
        this->attachToDevice.setGeometry(40,110,220,30);
        this->attachToDevice.setText("投屏到此设备");
        this->attachToDevice.setFont(QFont("Times New Roman"));
        this->attachToDevice.setStyleSheet(style_but);
        this->attachToDevice.show();
        // disconnect
        this->detachFromDevice.setParent(this);
        this->detachFromDevice.setGeometry(40,150,220,30);
        this->detachFromDevice.setText("停止投屏");
        this->detachFromDevice.setFont(QFont("Times New Roman"));
        this->detachFromDevice.setStyleSheet(style_but);
        this->detachFromDevice.show();

         --------------TODO绑定函数--------------
         投屏/结束投屏 因为多次绘制 所以需要首先 解绑 然后 再 绑定
        this->attachToDevice.disconnect();
        this->detachFromDevice.disconnect();
        connect(&this->attachToDevice, &QPushButton::clicked, this, &PhoneCard::onAttachToDevice);
        connect(&this->detachFromDevice, &QPushButton::clicked, this, &PhoneCard::onDetachFromDevice);

        this->usb.hide();
        this->wifi.hide();
    }
    else if(modType == 0){
        // to phone
        // wifi按键
        this->wifi.setParent(this);
        this->wifi.setGeometry(40,110,220,30);
        this->wifi.setText("Wifi 连接");
        this->wifi.setFont(QFont("Times New Roman"));
        this->wifi.setStyleSheet(style_but);
        this->wifi.show();
        // USB
        this->usb.setParent(this);
        this->usb.setGeometry(40,150,220,30);
        this->usb.setText("USB 连接");
        this->usb.setFont(QFont("Times New Roman"));
        this->usb.setStyleSheet(style_but);
        this->usb.show();
         --------------TODO绑定函数--------------
         解绑自己WiFi USB
         重新绑定

    }
    else if(modType == -1){
        this->attachToDevice.hide();
        this->detachFromDevice.hide();
        this->usb.hide();
        this->wifi.hide();
    }
}

即每次绑定前先disconnect 再重新 connect

当然 这个估计只适用于 对应一个槽函数,如果对应多个,可能就得每次重新绑定多个了~

其他BUG

修复 开始投屏后 点击刷新 “投屏中” 按钮被重置但是 状态不变的bug

 fungcard.h 里面加这几个内置函数
    QString getAttachStatus(){
        return this->attachToDevice.text();
        }
    QString getWifiStatus(){
        return this->wifi.text();
        }
    QString getUsbStatus(){
        return this->usb.text();
        }


 然后在mainmenu.cpp 点刷新时 重新判断一下  进行一个阻止
void MainMenu::onRefreshClicked() {
    // 根据文本决定当前状态
    int findType = -1;
    if(ToPhoneButton.text() == "已运行")
        findType = 1;
    else if(ToPCButton.text() == "已运行")
        findType = 0;
    else{
        QMessageBox::information(this,"JoyStick", "请先在左侧选择模式!",QMessageBox::Ok);
        return ;
    }

    for (PhoneCard &phone : phoneCards) {
        if(phone.getAttachStatus() == "投屏中..." || phone.getWifiStatus() == "投屏中..." || phone.getUsbStatus() == "投屏中...")
        {
            QMessageBox::information(this,"JoyStick", "有投屏还未停止,请停止后刷新!",QMessageBox::Ok);
            return;
        }
    }

    int number = 0;
    for (PhoneCard &phone : phoneCards) {
        phone.setParent(this);
        phone.drawOnRefresh(number++,findType);
    }
}

这里的启示就是 inline 来获取状态加以判断

你可能感兴趣的:(开发,c++,qt)