Denying subwindow change in QMdiArea - qt

I have a QMdiArea with several QMdiSubWindows. While construction, the QMdiArea is initialized as TabbedView and DocumentMode, so it looks like a QTabWidget by default.
When user clicks the tab to switch the sub windows, I want to add a confirmation, and if the user chooses cancel, the active tab will not be changed.
My solution is to handle the signal "subWindowActivated", and calls "setActiveSubWindow" back to the original sub window if the confirmation fails.
The problem is, the setActiveSubWindow call didn't work as expected. The active window is switched back to the original one, but the sub windows are no longer maximized, but overlapped. And the original sub window appears behind the user clicked one. I can call setWindowState to maximize the sub window, and set focus to it, but the sub window is still behind the user clicked one.
I want to know if there is proper way to switch back to the original sub window after user clicked a new one.
Below are some code pieces
MyClass::MyClass()
{
m_pMdiArea = new QMdiArea;
m_pMdiArea->setViewMode(QMdiArea::TabbedView);
m_pMdiArea->setDocumentMode(true);
connect (m_pMdiArea, SIGNAL(subWindowActivated(QMdiSubWindow *)), this, SLOT(subWindowActivate(QMdiSubWindow *)));
}
void MyClass::subWindowActivate(QMdiSubWindow * window)
{
if (!window || window == m_pCurrentWindow)
{
return;
}
if (ConfirmationRejected)
{
m_pMdiArea->setActiveSubWindow(m_pCurrentWindow);
return;
}
m_pCurrentWindow = window;
}

You should use at Cancel block:
activatePreviousSubWindow ();

Related

run time error opening a new window by selecting a table content

I have the following slot from a push-button click that opens a new window.
void StageTwoPatients::on_pushButton_Open_clicked()
{
QModelIndexList selection=ui->tableWidget->selectionModel()->selectedRows(0);
this-> close();
StageOneMain *newPatient = new StageOneMain(selection[0].data().toString(),this);
newPatient-> show();
}
Here user needs to select a row from a table and click pushbutton to open a new window.
If the user dont select a row accidentally and click the push button I get runtime error as shown here. How do I tell user using a QMessageBox that they should select a row before click push button?
Thanks in advance.
If the user dont select a row accidentally and click the push button I get runtime error
Simply do nothing if there is no selection.
void StageTwoPatients::on_pushButton_Open_clicked()
{
QModelIndexList selection=ui->tableWidget->selectionModel()->selectedRows(0);
if (selection.empty()){
return;
}
...
}

ZKOSS: onClick() working differently for mouse click vs mouse tap

I am trying to disable double clicking a Help label which is an anchor to open the Help window.
<p:a id="helpClick" onClick="help()">
<label value="Help" style="color:#FFFFFF;" />
</p:a>
When onClick() event is triggered once, either by Mouse click or Tapping the touchpad once, the help() method is being invoked.
void help() {
flag = true;
this.helpClick.setDisabled(true);
Window popupWindow = null;
popupWindow = (Window) Executions.createComponents("/zul/mainHelp.zul",
null, null);
this.popupWindow.setClosable(true);
popupWindow.addEventListener("onClose", new EventListener() {
void onEvent(Event event) throws Exception {
this.helpClick.setDisabled(false);
}
});
}
is the code which i added to handle the anchor tag with the id helpClick.
This is working perfectly fine when i use mouse clicks. For the first click, the window gets opened and simultaneously the Label is not taking any more click events.
When i try the same with mouse tap(using the touchpad), two single clicks are being triggered.
I have used onClick() to capture the event.
I am trying to disable the Label once it is clicked and the window is opened. Only after the window gets closed, i am enabling the label.
This is working totally fine when i use mouse clicks but not when i use tap.
With tapping, the label is taking multiple clicks which isnt the case with Mouse Click.
Without seeing code it's difficult to provide advice but maybe you can capture the onDoubleClick event and ignore it or forward it to the to the same listener as your onClick event.
... forward="onClick=onHelpClick,onDoubleClick=onHelpClick" ...
After question edit:
It sounds like a bug if you can double click a disabled component. One thing you could try is set your link to autodisable <p:a id="helpClick" onClick="help()" autodisable="self"> as per A component documentation

I tried to disable the button after the first click.it's working for me now. when the dialog box closed i want the button enabled again

void CMyViewerDlg::OnBnClickedShow()
{
m_DCM.Create(IDD_TAG, this);
m_DCM.ShowWindow(SW_SHOW);
BOOL isOn = true;
if (isOn == false)
{
GetDlgItem(IDC_SHOW)->EnableWindow(TRUE);
}
else
{
GetDlgItem(IDC_SHOW)->EnableWindow(FALSE);
}
}
now the button is disabled after the first click..i want to enable the button again when i close the dialog box..help me to slove this problem..
This is not meant to replace the existing answer but it is based on the comments.
You can use the Classwizard to add custom message handlers. I create a GUID and attach that to WM_APP and use registered messages.
This is a non modeless dialog. So you have to keep track of it on your own. The easiest way would be to send a WM_APP+n message to the parent window/dialog, when the dialog receives the OnDestroy or OnClose event. The parent dialog can handle this message and disable the appropriate button.
Another method is just setting a timer. In the timer you can check if m_DCMTagDlg.m_hWnd is still not NULL. If it reaches NULL just disable the button.

Dragging a radio button performes the action but doesn't set the check

I am creating an application that should work on desktop and some mobile platforms.
The following example creates and connects my portrait/landscape buttons, in a group, to a slot, on the release signal.
m_landscapeRadio = new QRadioButton(QObject::tr("Landscape "));
m_portraitRadio = new QRadioButton(QObject::tr("Portrait "));
m_orientationGroup.addButton(m_landscapeRadio, 0);
m_orientationGroup.addButton(m_portraitRadio, 1);
m_orientationGroup.setExclusive(true);
m_landscapeRadio->setChecked(true);
connect(&m_orientationGroup, SIGNAL(buttonReleased(int)), this, SLOT(orientationSlot(int)));
But I found a weird situation:
Assume landscape button is checked. If I press and drag away from the portrait radio button, the slot action is performed (for the portrait option) but the portrait button is not checked.
I would like the action not to be performed.
For now...
In the orientationSlot I test the argument and set the checked value myself... Though I really expected the buttons to know to do that themselves.
But I think it is more expected by users that, if the press a button and change their mind, to be able to drag away from the button and not have the action be performed.
I can handle verifying if the check really happened in the action slot, and either check or discard the action depending on how I will think the user experience is better...
If I want the buttons to be checked and to perform the action as well:
void MyWidget::orientationSlot(int checked)
{
if(checked) m_portraitRadio->setChecked(true);
else m_landscapeRadio->setChecked(true);
.... actual actions
}
If I want the action not to be performed when the user drags away from the button (my preferred option):
void MyWidget::orientationSlot(int checked)
{
if(m_orientationGroup.checkedId() != checked) return;
.... actual actions
}
I use QRadioButton and handle mouse button being released event for reacting
on radio button being switched. It causes problems altogether with dragging event. I would like to either get the button
to be checked, or the action not to be performed.
http://doc.qt.io/qt-5/qradiobutton.html
Whenever a button is switched on or off, it emits the toggled()
signal. Connect to this signal if you want to trigger an action each
time the button changes state. Use isChecked() to see if a particular
button is selected.
Either you connect the radio button to the handler explicitly or the whole group: http://doc.qt.io/qt-5/qbuttongroup.html#buttonToggled
void QButtonGroup::buttonToggled(QAbstractButton *button, bool
checked)
This signal is emitted when the given button is toggled. checked is
true if the button is checked, or false if the button is unchecked.
Note: Signal buttonToggled is overloaded in this class. To connect to
this one using the function pointer syntax, you must specify the
signal type in a static cast, as shown in this example:
connect(buttonGroup, static_cast<void(QButtonGroup::*)
(QAbstractButton *, bool)>(&QButtonGroup::buttonToggled),
[=](QAbstractButton *button, bool checked) {
if (button == m_portraitRadio) {
// Portrait (un)checked
if (checked)
{
// checked!
}
}
/* ... */ });

How to detect QTableWidget scroll source (code itself/user (wheel)/user (scrollbar))?

I'm writing a program using Qt 4.8 that displays a table (QTableWidget) filled with filenames and file's params. First an user adds files to the list and then clicks process. The code itself updates the contents of the table with simple progress description. I want the table by default to be scrolled automatically to show the last processed file and that code is ready.
If I want to scroll it by hand the widget is being scrolled automatically as soon as something changes moving the viewport to the last element. I want to be able to override the automated scroll if I detect that it was the user who wanted to change view.
This behavior can be seen in many terminal emulator programs. When there's a new line added the view is scrolled but when user forces the terminal to see some previous lines the terminal does not try to scroll down.
How could I do that?
Solution:
I created an object which filters event processed by my QTableWidget and QScrollBar embedded inside. If I spot the event that should turn off automatic scrolling I just set a flag and stop scrolling view if that flag is set.
Everything is implemented inside tableController class. Here are parts of three crucial methods.
bool tableController::eventFilter(QObject* object, QEvent* event)
{
switch (event->type())
{
case QEvent::KeyPress:
case QEvent::KeyRelease:
case QEvent::Wheel:
case QEvent::MouseButtonDblClick:
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
_autoScrollEnabled = false;
default:
break;
}
return QObject::eventFilter(object, event);
}
void tableController::changeFile(int idx)
{
[...]
if (_autoScrollEnabled)
{
QTableWidgetItem* s = _table.item(_engine.getLastProcessed(), 1);
_table.scrollToItem(s);
}
[...]
}
void tableController::tableController()
{
[...]
_autoScrollEnabled = true;
_table.installEventFilter(this);
_table.verticalTableScrollbar()->installEventFilter(this);
[...]
}
Thanks for all the help. I hope somebody will find it useful :)
Subclass QTableWidget and overload its wheelEvent. You can use the parameters of the supplied QWheelEvent object in order to determine if the user scrolled up or down.
Then use a simple boolean flag which is set (or reset) in your wheelEvent override. The method which is responsible for calling scrollToBottom() should then consider this boolean flag.
You will have to find a way to figure out when to set or reset that flag, e.g. always set it when the user scrolls up and reset it when the user scrolls down and the currently displayed area is at the bottom.
connect(_table->view()->verticalScrollBar(), &QAbstractSlider::actionTriggered, this, [this](int) {
_autoScrollEnabled = false;
});

Resources