Qt:在发出信号之前发出消息(Qt: Issuing a message before emitting a signal)

编程入门 行业动态 更新时间:2024-10-24 18:24:42
Qt:在发出信号之前发出消息(Qt: Issuing a message before emitting a signal)

我的申请和我的问题的简要说明:

在QTabWidget中,我有几个包含每个2个QRadioButtons的groupBox。 例如,选择菜单(groupBox):( RadioButton :) A(RadioButton :) B

在某个时间点,只有一个菜单可以处于活动状态。 两个radioButton都连接到其他 - >当我点击radioButton A并将其设置为true时,radioButton B会自动设置为false - 反之亦然。

在尝试更改菜单设置时,在发出单击信号之前,我想发出一个QMessageBox警告“您确定要更改菜单吗?这可能会对您的设备造成严重损坏。” - 是/否。

单击是时,我想更改菜单设置。 点击“否”时,我希望一切都像以前一样。

我唯一的问题是:在on_radio_button_toggled插槽中发出QMessageBox时,radioButton状态已经更改为true。 即使我再次更改插槽中的状态并更正它们,当弹出消息显示时,状态似乎已经改变。 我不希望这样,因为这意味着menue的状态已经改变了。

在发出实际信号槽之前,如何或如何让QMessageBox弹出 - 单击单选按钮?

非常感谢您的帮助。

更新:我现在已经按照建议实现了eventFilter。 这是我的源代码:

ui->radioButtonMenu1->installEventFilter(this); ui->radioButtonMenu2->installEventFilter(this);

SubmenuOne是一个QWidget。 它通过QTabWidget(通过占位符)集成到MainWindow中。

bool SubmenuOne::eventFilter(QObject *obj, QEvent *event) { if(event->type() == QEvent::MouseButtonPress) { QMessageBox::StandardButton reply; reply= QMessageBox::question(this,tr("WARNING"),tr("Changing the settings may cause severe damage to your device! Please confirm your decision."),QMessageBox::Yes|QMessageBox::No); if (reply == QMessageBox::Yes) { //QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); //keyEvent->accept(); //event->accept(); qDebug("Yes."); return false; } else { qDebug("No."); return true; } } }

Brief description of my application and my question:

In a QTabWidget I have several groupBoxes containing each 2 QRadioButtons. E.g. Select Menu (groupBox): (RadioButton:) A (RadioButton:) B

At one point of time, only one Menu can be active. Both radioButtons are connected to each othe -> When I click radioButton A and set it true, radioButton B is automatically set false - and the other way round.

When trying to change the menu setting, before a click signal is emitted, I would like to issue a QMessageBox Warning "Are you sure you want to change the menu? This can cause severe damage to your device." -Yes/No.

When clicking Yes, I would like to change the menue setting. When clicking No, I would like everything to remain as before.

The only problem I have is: When issuing the QMessageBox in the on_radio_button_toggled slot, the radioButton state has already changed to true. Even if I change the state in the slot again and correct them, it looks like the state has already changed when the pop up message shows up. I don't want that because that implies that the state of the menue has already changed.

Where or How can I let a QMessageBox pop up before emitting the actual signal slot - when clicking the radio Button?

Thank you very much for your help.

Update: I have now implemented an eventFilter as recommended. Here is my source code:

ui->radioButtonMenu1->installEventFilter(this); ui->radioButtonMenu2->installEventFilter(this);

SubmenuOne is a QWidget. It is integrated into the MainWindow by a QTabWidget (via placeholder).

bool SubmenuOne::eventFilter(QObject *obj, QEvent *event) { if(event->type() == QEvent::MouseButtonPress) { QMessageBox::StandardButton reply; reply= QMessageBox::question(this,tr("WARNING"),tr("Changing the settings may cause severe damage to your device! Please confirm your decision."),QMessageBox::Yes|QMessageBox::No); if (reply == QMessageBox::Yes) { //QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); //keyEvent->accept(); //event->accept(); qDebug("Yes."); return false; } else { qDebug("No."); return true; } } }

最满意答案

你必须使用bool eventFilter(QObject *obj, QEvent *event); 在窗口中声明事件过滤器,然后将事件过滤器安装到每个radiobutton,如下所示: radioButton1->installEventFilter(this); 。 然后在eventFilter检查事件类型: if (event->type() == QEvent::MouseButtonPress)并显示您的QMessageBox 。 然后您可以接受事件并根据用户选择返回true或false 。

bool SubmenuOne::eventFilter(QObject *obj, QEvent *event) { if(event->type() == QEvent::MouseButtonPress) { QMessageBox::StandardButton reply; reply= QMessageBox::question(this,tr("WARNING"),tr("Changing the settings may cause severe damage to your device! Please confirm your decision."),QMessageBox::Yes|QMessageBox::No); if (reply == QMessageBox::Yes) { static_cast<QRadioButton*>(obj)->setChecked(True); } event->accepted(); return true; } return QMainWindow::eventFilter(obj, event); //you forget this. QMainWindow is just base class of your SubmenuOne. }

You have to use bool eventFilter(QObject *obj, QEvent *event); Declare event filter in your window, then instal event filter to every radiobutton like this: radioButton1->installEventFilter(this);. Then at eventFilter check event type: if (event->type() == QEvent::MouseButtonPress) and show your QMessageBox. Then you can accept event and return true or false depending on user choise.

bool SubmenuOne::eventFilter(QObject *obj, QEvent *event) { if(event->type() == QEvent::MouseButtonPress) { QMessageBox::StandardButton reply; reply= QMessageBox::question(this,tr("WARNING"),tr("Changing the settings may cause severe damage to your device! Please confirm your decision."),QMessageBox::Yes|QMessageBox::No); if (reply == QMessageBox::Yes) { static_cast<QRadioButton*>(obj)->setChecked(True); } event->accepted(); return true; } return QMainWindow::eventFilter(obj, event); //you forget this. QMainWindow is just base class of your SubmenuOne. }

更多推荐

本文发布于:2023-07-29 20:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:信号   消息   Qt   Issuing   signal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!