Qt项目开发中所遇

 讲述下面代码所表示的含义:

  QWidget widget_19 = new QWidget();  
  QVBoxLayout *touchAreaLayout = new QVBoxLayout(widget_19);
  QWidget *buttonArea = new QWidget(widget_19);

1、新建一个名为widget_19的QWidget,将给其应用垂直管路布局。

2、新建一个名为buttonArea的QWidget,作为子窗口加入到widget_19里,同时继承widget_19的样式。同时QVBoxLayout作为父窗口widget_19的布局管理器可以自动管理子窗口buttonArea的位置和大小。资源管理方面符合对象树机制,父窗口widget_19销毁时,子窗口buttonArea所有子控件的资源会被自动释放。

项目中单例设计模式的应用

我有一个设备管理类HidManger,包含设备初始化,连接设备,接收报告,发送报告等方法。

现在我主函数想要访问这些方法,需要实例化一个设备管理对象HidManger *hidmanger=new HidManger(this)。

同时我的其他一些模块也需要通过判断HidManger对象是否销毁从而判断当前设备是否连接。

因此我在HidManger类实例化唯一的一个 HidManger对象 ,在主函数和其他模块获取这个实例即可。

项目中继承和多态的使用

列如我的程序主界面有按钮和旋钮两个模块:

按钮模块对话框如下:

现在我旋钮模块的对话框想复用一下按钮模块的对话框,保留一些选项,再增加旋钮自己的选项,如下:

信号槽问题

最近遇到一个问题,我绑定了信号和槽,但是当信号被触发时,槽函数无论如何都不会被调用。

如下所示传参创建一个对话框:


 BingShortcutKey2(btnId, DialogType::RotaryCombo); // 组合键类型
void MainWindow::BingShortcutKey2(quint8 rotaryId, DialogType type) {
  
    switch (type) {

    case DialogType::RotaryCombo: {
        // 按钮+旋钮组合键对话框(新增界面)
        RotaryCombinationDialog comboDialog(rotaryId, this);
        if (comboDialog.exec() != QDialog::Accepted) return;
        break;
    }
    default:
        qWarning() << "未知对话框类型";
        return;
    }
}

发送信号部分:

    if (currentData.buttonId != 0 && currentData.rotaryId != 0) {
        // 组合报告:同时包含按钮ID和旋钮ID
        emit combinationKeyReceived(currentData.buttonId, currentData.rotaryId);
    } 

 信号槽的绑定以及槽函数定义:

 connect(
            hidManager, &HidManager::combinationKeyReceived,
            this, &RotaryCombinationDialog::handleRoatryId,
            Qt::QueuedConnection
            );

void RotaryCombinationDialog::handleRoatryId(int receivedButtonId, int receivedRotaryId) {
    if (receivedButtonId != m_buttonId) {
        qDebug() << "忽略不匹配的按钮ID:" << receivedButtonId << "(期望:" << m_buttonId << ")";
        return;
    }

    QPair<int, int> key = qMakePair(receivedButtonId, receivedRotaryId);
    QKeySequence shortcut = m_combinationShortcuts.value(key, QKeySequence());

    if (!shortcut.isEmpty()) {
        simulateSystemShortcut(shortcut);
        qDebug() << "执行按钮ID" << receivedButtonId
                 << "和旋钮ID" << receivedRotaryId
                 << "的快捷键:" << shortcut.toString();
    } else {
        qDebug() << "未找到按钮ID" << receivedButtonId << "和旋钮ID" << receivedRotaryId << "的快捷键配置";
    }
}

当combinationKeyReceived信号被触发时,我发现无论如何handleRoatryId都没有被调用。

实际上原因就是RotaryCombinationDialog被销毁了。在BingShortcutKey2()里创建RotaryCombinationDialog对象的时候是局部对象,过了BingShortcutKey2()对象就销毁了,所以信号发送给RotaryCombinationDialog,没有办法去接收。

像下面这种全局对象就可以调用信号和槽

//mainwindows.h
 std::unique_ptr<RotaryCombinationDialog> rotaryCombinationDialog;
//mainwindows.cpp
void MainWindow::BingShortcutKey2(quint8 rotaryId, DialogType type) {
    case DialogType::RotaryCombo: {
        // 按钮+旋钮组合键对话框
        rotaryCombinationDialog = std::make_unique<RotaryCombinationDialog>(rotaryId, this);
             if (rotaryCombinationDialog->exec() != QDialog::Accepted) return;
        break;
    }
    default:
        qWarning() << "未知对话框类型:" << (int)type;
        return;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孙鹏宇.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值