Please take a look at this picture:
You can see a QTableView with some stupid content and a context menu in the center of it. My issue is if I click the table (no matter which button was pressed) view when that context menu is on (and I expect the context menu to disappear as it happens in Windows program and then appear back in a new place if the right button was pressed) my program immediately crash. I create it like this:
connect(tableView, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(showContextMenu(const QPoint&)));
void MainWindow :: showContextMenu(const QPoint &_point)
{
QPoint pos = tableView->mapToGlobal(_point);
QModelIndex index = tableView->currentIndex();
int row = index.row();
QMenu menu;
menu.addAction("Test 1");
menu.addAction("Test 2");
QAction *action = menu.exec(pos);
QString text = action->text();
if (text == "Test 1")
qDebug("Test 1");
else
if (text == "Test 2")
qDebug("Test 2");
else
qDebug("Vzdroch");
}
I have no idea why it crashes. There's no such thing as debugger in QtCreator, i.e. it is but installing it is as complicated as launching a rocket to space. What I need is just to handle mouse clicks beyond context menu area as I normally do.
I understand that it might be really difficult for you to find out why it crashes, so I'm gonna ease my question a bit. Is there a way to make that context menu disappear when the mouse goes beyond its area? There's a signal named hovered() in Qt. It is emitted when user mouse is over a widget, so I was searching for a signal, let's call it unhovered(), emitted when user takes mouse off a widget. Unfortunately I failed to find such a signal. Is there a way to let my program know that mouse is off?
Hope I've described my issue fully.
QMenu::exec returns 0 if no menu item was selected.
You need to check action before you dereference it, otherwise you'll dereference a null pointer which leads to undefined behavior.
QAction *action = menu.exec(pos);
if (!action) {
qDebug() << "no menu selected";
} else {
QString text = action->text();
...
}
Related
I am using QSlider element in my app as a toogle switch. I took this project over from a colleage of mine who left. So far so good, except, that this switch has a black label around it. I can't really change the style and rework everything so, I am stuck with label and slider as switch.
I need to handle events from button clicked and button released. I do it like this-
bool CustomSlider::eventFilter(QObject *obj, QEvent *ev )
{
if (ev->type() == QEvent::MouseButtonRelease)
{
changeSliderValue();
}
if(ev->type() == QEvent::MouseButtonPress){
lastSaved = ui->horizontalSlider->value();
}
return QWidget::eventFilter(obj,ev);
And changeSliderValue()-
void CustomSlider::changeSliderValue()
{
int current = ui->horizontalSlider->value();
if(lastSaved==current){
if(current){
ui->horizontalSlider->setValue(0);
}else{
ui->horizontalSlider->setValue(1);
}
}
}
So, in short, if I click, it saves state of slider and when releasing it checks against saved state. I do it because QSlider has button pressed signal as well. I have put debug messages in my code and funny thing is debug messages fire. So my code works. If I call ui->horizontalSlider->value() I get the correct(changed) value But the slider is not moving. I have tried checking sequences, if it does not change value twice(it does not), I have tried calling repaint() and so far nothing works. Except if don't minimize(via minimize button) or click in different app(like web browser) and then I can see that slider moves to correct postion. I am completely confused. Has anyone encoutered something like this before?
I am doing a small game in Qt and try to use WASD to move my protagonist(QGraphicsPixmapItem). The map is quite big so I use a QComboBox to change the scale of the scene.
The game looks like this:
The simple game
How I built the protagonist:
protagonist = new MyProtagonist();
protagonist->setFlag(QGraphicsItem::ItemIsFocusable);
protagonist->setFocus();
scene->addItem(protagonist);
How I built the combox:
sceneScaleCombo = new QComboBox;
QStringList scales;
scales << tr("1%")<<tr("10%") << tr("20%") <<tr("50%") << tr("100%") <<tr("200%");
sceneScaleCombo->addItems(scales);
sceneScaleCombo->setCurrentIndex(4);
connect(sceneScaleCombo,SIGNAL(currentIndexChanged(QString)),this,SLOT(sceneScaleChanged(QString)));
void MainWindow::sceneScaleChanged(const QString &scale)
{
double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100;
QMatrix oldMatrix = view->matrix();
view->resetMatrix();
view->translate(oldMatrix.dx(),oldMatrix.dy());
view->scale(newScale,newScale);
protagonist->setFocus();
}
Everything worked well at the beginning. However, after I clicking the combobox, my protagonist cannot be controlled by keyboard any more. I need to click my protagonist to make it focused again.
Is there any way to set it focused automatically?
Thanks a lot.
By clicking QComboBox the QGraphicsView looses the focus. You attempt to return it back by calling setFocus(); on the QGraphicsPixmapItem. This approach doesn't work as you expect, because it sets the focus item inside the view, but the view itself still does not have focus. To fix that in MainWindow::sceneScaleChanged instead of protagonist->setFocus(); write view->setFocus();.
I have a QMenu for which I've created a QColorModel action widget (It's effectively just a QStandardItemModel). My desired behavior is that when a user clicks one of the colors in the model, that the action should trigger, and the menu close. However, it doesn't seem to do that, even when I trigger the action manually.
I've tried manually hiding the menu, but it's a kludge because it won't hide parent menus which th menu may be attached to.
Here's the relevant section of code:
// color menu
m_colorMenu = new QMenu("color", this);
m_colorView = new QColorView(m_colorMenu);
m_colorViewAction = new QWidgetAction(m_colorMenu);
m_colorViewAction->setDefaultWidget(m_colorView);
m_colorView->setModel(new QStandardColorModel);
connect(m_colorView, &QColorView::clicked, [&](QModelIndex index)
{
QColor color = qvariant_cast<QColor>(index.data(Qt::DecorationRole));
if (m_pen.color() != color)
{
m_pen.setColor(color);
drawIcon();
drawColorIcon();
update();
}
//this->hide(); // kludge, didn't close all parent menus
m_colorViewAction->trigger(); // doesn't seem to cause menu closure
});
m_colorMenu->addAction(m_colorViewAction);
EDIT
I've also tried adding something to the effect of:
QMenu* menu = m_colorMenu;
do
{
menu->close();
menu = dynamic_cast<QMenu*>(menu->parent());
} while (menu);
but it also is fragile/kludgey because it assumes a) all widgets are properly parented, and b) that all the parents are actually supposed to be menus. In my case, they aren't.
If the containing menus aren't in the parentage tree, and the menu you want to close isn't the top level menu, there is no easy way to do this. That said, there is:
THE NUCLEAR OPTION
Adding this to the end of the lambda function
auto topLevelWidgets = qApp->topLevelWidgets();
for (auto widget : topLevelWidgets)
{
QMenu* menu = dynamic_cast<QMenu*>(widget);
if (menu)
{
menu->close();
}
}
will cause ALL top level menus to close once the action is triggered. This is a relatively OK way to accomplish what you want because:
one of the top level menus will contain the menu in question, and
never say never, but I can't think of a single case where you would have (or want) more than one menu open at a time, so most likely the only open menu tree you would close is the intended one.
I've stumbled across very strange behaviour during work on my program.
I've written custom changeEvent class, which allows me to hide program to SysTray on minimizing.
But when i double click on taskbar app icon, the function goes crazy. It creates 2 to 4 systray icons and on requesting window show again, it just shows main window borders without any content inside.
Here's my changeEvent code:
void MainWindow::changeEvent(QEvent *e) {
QMainWindow::changeEvent(e);
if(e->type()==QEvent::WindowStateChange)
if(isMinimized()) {
trayIcon=new QSystemTrayIcon(QIcon(":/icon/itime.ico"));
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(on_show(QSystemTrayIcon::ActivationReason)));
QAction *showAction=new QAction("Pokaż",trayIcon);
connect(showAction,SIGNAL(triggered()),this,SLOT(on_show()));
QMenu *trayIconMenu=new QMenu;
trayIconMenu->addAction(showAction);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->show();
this->hide();
}
}
on_show(QSystemTrayIcon::ActivatioReason) SLOT:
void MainWindow::on_show(QSystemTrayIcon::ActivationReason reason) {
if(reason) {
if(reason!=QSystemTrayIcon::DoubleClick)
return;
}
if(this->isMinimized()) {
this->raise();
this->showNormal();
this->setWindowState(Qt::WindowActive);
trayIcon->hide();
}
}
on_show() SLOT is just the same besides that first if.
Soo, I would like to know whether there is any way to disable minimizing of window by taskbar icon click.
If there's none, then maybe you have any ideas what can go wrong in here when doubleclicking on icon in taskbar?
Thanks for help!
I've managed to work around that problem by overloading closeEvent function and leaving alone changeEvent function.
So, I'm using boolean flag to distinct between closing of program by menu item and by clicking "X" button and the rest stays just the same, as posted in my earlier post with one change.
I've moved this whole block of code to window constructor in order to prevent multiple creation of trayIcon, as pointed out by Nicolas.
trayIcon=new QSystemTrayIcon(QIcon(":/icon/itime.ico"));
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(on_show(QSystemTrayIcon::ActivationReason)));
QAction *showAction=new QAction("Pokaż",trayIcon);
connect(showAction,SIGNAL(triggered()),this,SLOT(on_show()));
QMenu *trayIconMenu=new QMenu;
trayIconMenu->addAction(showAction);
trayIcon->setContextMenu(trayIconMenu);
Thanks for your help!
I have a list of strings during run time.
Any one help me to display these strings in QWidget. When I right click that string I need to have a option show index that will show index of that string in QMessageBox.
If it is possible means give some technical guidance.
Thank you.
OK, so let us start with the design for your use case...
I would recommend using a QListWidget for a list. Each string could be a separate item.
You could show a popup for right click, but if it only has the show index action, it does not really make that much sense on its own. You could just show the messagebox with that index right away.
I would write something like this below:
MyClass::MyClass(QObject *parent)
: QObject(parent)
, m_listWidget(new QListWidget(this))
{
QStringList myStringList = QStringList() << "foo" << "bar" << "baz";
m_listWidget->addItems(myStringList);
// Set up your action with the popup for right click if needed
// and connect to the "triggered" signal of the particular action
connect(listWidget, SIGNAL(itemClicked(QListWidgetItem * item)), SLOT(showMessageBox(QListWidgetItem * item)));
...
}
void MyClass::showMessageBox(QListWidgetItem * item)
{
Q_UNUSED(item)
QMessageBox messageBox;
messageBox.setText(m_listWidget->currentRow());
messageBox.exec();
}
If you have more actions for the right click, you could use a popup dialog, yes, with several actions placed onto, but so far, that does not seem to be the use case in here.