Make QWidget disappears after click not on it - c++

I'm showing QTableWidget, and want it to disappear after some item in this table was selected and if user clicked outside QTableWidget area.
connect(tableWidget, SIGNAL(itemClicked(QTableWidgetItem *)), tableWidget, SLOT(close()));
this line do what I want after selecting item. Is it a way of make widget disappears after clicking not on it without subclassing it (I can subclass and write my own losefocus event handler, for example, but without subclassing would be better)?

let's assume, you have MainWidget, that contains everything within it. It has clicked() signal. Connect that to some slot and in that slot hide tableWidget ( tableWidget()->hide() )

This is probably not a very elegant solution, but it might work:
Subclass QTableWidget
Make sure that the table widget has the keyboard focus while you display it.
Reimplement void QWidget::focusOutEvent ( QFocusEvent * event ) (close the table widget, when you lose focus)

My solution was to put QTableWidget into QWidget and made the latter Qt::Popup - an it acts exactly how I need

Related

intercept and ignore a wheel event in a QTabWidget

I implement a UI with Qt. My UI is a mainwindow containing some widgets, and a supervisor to manage my widget behaviors. In one of my widget, outputDataWidget, I have a QTabWidget, and some other widgets.
In my tabWidget, I have a dataEditor to show points, and I create a "+" tab, to create a new tab. The tab creation is done by a connect function, which is in the mainwindow:
connect(outputWidget.get(), SIGNAL(SIG_createNewOutput(int)), projectSupervisor.get(), SLOT(sCreateNewOutput(int)));
My problem is: when we use the wheel mouse button in the tab header to go rigth, some new page are created. I would like intercept the signal and avoid the creation of a new tab.
I try to add a filter event in the mainwindow and in the outputdatawidget to ignore the wheel event. Then I try to reimplemeted wheelEvent(QWheelEvent* we) and event(QEvent *e) of the QTabWidget. Each time, we enter in thiese modification after call the connect (create the tab).
Have you an idea how to call the event before the connect, or how I can disable the wheelevent of a QTabWidget?
Thanks
You'll have to subclass QTabBar to handle the QWheelEvent, then call QTabWidget::setTabBar during initialization.
This is necessary because QTabWidget internally uses a QTabBar with Qt::WheelFocus, so the wheel event never propagates to the tab widget.

Qt connecting two signals and one slot

I have a programm with a QLabel, QTextEdit and a QPushButton.
I want to put the text from LineEdit to Label when I click the button.
I can do that by creating my own slot but can it be done with Qt slots?
I've tried this code but it works not as I want...
this->connect(pushButton ,SIGNAL(clicked()), lineEdit, SIGNAL(textChanged(QString)), Qt::QueuedConnection);
t->connect(lineEdit, SIGNAL(textChanged(QString)) , label ,SLOT(setText(QString)), Qt::DirectConnection);
If you need to force the user to push a QButton for "applying" the text he/she typed in a QTextEdit to a QLabel, maybe you want to check the validity of the inserted text, or use the text to achieve some goal or to store it in a variable for later use... so you need a custom slot or a custom class.
Instead you can connect the signal QTextEdit.textChanged(QString) to the slot QLabel.setText(QString), so everything is typed in the QTextEdit is sent to the QLabel without pushing a button.
But all depends on your aim.
Here's how I would do it:
connect(ui->pushbutton, SIGNAL(clicked()), this, SLOT(slot_pushbutton_clicked()))
And then in the the slot_pushbutton_clicked slot,
ui->label->setText(ui->lineEdit->text)
Hope it helps :)

Reusing a QMenu within multiple Qmenu

Hi all,
I have some code generating a dynamically filled QMenu depending on some data (I will call it thisMenu). The QMenu thisMenu is created, taking some "main" QWidget as parent and is added to a QMenuBar within this QWidget (menuBar.addMenu(&thisMenu). Latter on, I want the user to be able of accessing thisMenu from a context menu (the user right click on some portion of the QWidget, which pops a QMenu (called contextMenu) with some actions, and the previous QMenu as a sub-menu).
If I reuse the QMenu that I first created with contextMenu.addMenu(&thisMenu) I find out that, even if contextMenu pops at the right global position, thisMenu is always translated to some other position and appearing sometimes above, sometimes under contextMenu.
I can test that this is linked to the parenting chain : thisMenu is not a child of contextMenu, if I create it a child of contextMenu, everything is fine. Is there a way of cleanly handling this case without recreating a QMenu similar to thisMenu, or changing the parent of thisMenu; i.e. reusing thisMenu in both QMenuBar and in some context menu/QMenu? In other what is the proper way of handling parenting chain for QMenu and sharing QMenu?
Thank you,
In other what is the proper way of handling parenting chain for QMenu and sharing QMenu?
You cannot share a QMenu across multiple places -- each QMenu can only exist in one place at a time. You should create separate QMenus: One for your menu bar and one for your context menu.
A simple way is to put your menu-generating code in a for-loop, to create multiple identical menus.
May I ask why you want to reuse your QMenu?
I can test that this is linked to the parenting chain : thisMenu is not a child of contextMenu
Yes, that is described in the documentation. When you add one QMenu to another, the parent doesn't change: http://qt-project.org/doc/qt-5/QMenu.html#addMenu
if I create it a child of contextMenu, everything is fine.
The position of a widget is always painted in a position relative to its parent. (Remember: A QMenu is a QWidget)
Following JKSH's answer, I decided to use a function to duplicate QMenu, without duplicating the QAction in it (they are not inheriting QWidget), hence conserving all established connections :
void duplicateMenu(QMenu* dst, QMenu& origin)
{
QMenu* sub = dst->addMenu(origin.title());
QList<QAction*> actions=origin.actions();
for(QList<QAction*>::iterator it=actions.begin(); it!=actions.end(); it++)
{
QMenu* itMenu = (*it)->menu();
if(itMenu!=NULL)
duplicateMenu(sub, *itMenu);
else
sub->addAction(*it);
}
}

How to prevent closing QMessageBox after clicking a button

I have 3 buttons on QMessageBox added by QMessageBox::addButton() method. Is it possible to prevent closing the message box if a button has been clicked? By default every button closes the window, but I don't want to do it for one button.
One interesting way to approach it that worked for me is to completely disconnect the signals for the target button created, and then re-add the intended functionality. This won't work for everyone, especially if the button isn't created this way and/or you still want to close the dialog correctly. (There might be a way to add it back and/or simulate the behavior with QDialog::accept, QDialog::reject, QDialog::done - haven't tried yet.)
Example:
QMessageBox *msgBox = new QMessageBox(this);
QAbstractButton *doNotCloseButton = msgBox->addButton(tr("This button will not close anything"), QMessageBox::ActionRole);
// Disconnect all events - this will prevent the button from closing the dialog
doNotCloseButton->disconnect();
connect(doNotCloseButton, &QAbstractButton::clicked, this, [=](){ doNotCloseButton->setText("See? Still open!"); });
If you can get a pointer to the QMessageBox widget, you can try to install a QObject::eventFilter on it which filters the QEvent::Close.
Just had the same problem but I wanted to add a checkbox and it kept closing the dialog on clicked even with the ButtonRole set to QMessageBox::ActionRole (tried others too). For this scenario I just called blockSignals(true) on the QCheckBox and now it allows check/uncheck behaviour without closing the dialog. Luckily QCheckBox works fine without signals but assume you want a signal from your button.
They should likely add a new role that doesn't close the dialog as it's a pain to derive a class for simple customizations.
I looked through the addButton() functions overloads, but there is no custom behavior for the buttons you add with this method. They will behave like the standard buttons on a messagebox should.
However if you want to create a fully customizable dialog, then your best option is to extend the QDialog class and use whatever controlls you like on it.
Thanks to #Albert's Answer, I found that this also possible in python:
messagebox = QMessageBox()
button = QPushButton("This button will not close anything")
messagebox.addButton(button, QMessageBox.ButtonRole.NoRole)
button.disconnect()

Problem with event handling on QToolButton in Linux

I am developing an application, I have added a QToolBar object in that, and have added the QToolButton object on that, I have also connect the clicked() event with that but the problem is that the mouse click event don't work on QToolButton but when I bring focus on that using Tab, then space button works fine, but I want it with mouse click.. any idea? here is the code.
pToolBar = new QToolBar(this);
pToolBar->setAllowedAreas(Qt::NoToolBarArea);//NoToolBarAreaAllToolBarAreas
pToolBar->setFloatable(false);
pToolBar->setGeometry(300,0,160,30);
QToolButton *playButton=new QToolButton(pToolBar);
playButton->setIcon(QIcon("/images/play.png"));
playButton->setGeometry(10,0,40,30);
playButton->setToolTip("Play/Pause");
connect(playButton, SIGNAL(clicked()),SLOT(playButtonClicked()));
The Tool buttons are normally created when new QAction instances are created with QToolBar::addAction() or existing actions are added to a toolbar with QToolBar::addAction().
Example:
QAction *newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
newAct->setShortcut(tr("Ctrl+N"));
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAct);
You can use triggered signal, This signal is emitted when the given action is triggered.
Your example:
QToolButton *playButton=new QToolButton(pToolBar);
connect(playButton, SIGNAL(triggered()),SLOT(playButtonClicked()));
Try explicitly adding the toolbutton to the toolbar. The following code works perfectly for me:
QToolBar *pToolBar = new QToolBar(this);
QToolButton *playButton=new QToolButton(pToolBar);
playButton->setIcon(QIcon("/images/play.png"));
playButton->setText("Play");
playButton->setToolTip("Play/Pause");
playButton->setGeometry(10,0,40,30);
QAction *a = pToolBar->addWidget(playButton);
a->setVisible(true);
connect(playButton, SIGNAL(clicked()),SLOT(playButtonClicked()));
You should probably save the QAction pointer somewhere, since it's the easiest way to assign keyboard shortcuts, enable / disable the button etc. Let me know if this works for you. If it doesn't, perhaps posting a complete compilable example here will help us help you. You should be able to get a small demo program that shows your problem within one or two files.
Cheers,
As jordenysp indirectly explains, the API is QAction centric