Set position (to right) of Qt QPushButton popup menu - c++

I am writing a popup menu for a Qt push button widget. Whenever the push button is clicked, a menu pops up (below the push button).
The popup menu is left-sided below by default.
Are there any ways to make the popup menu to pop up on the right side below the push button?
There is no set position function, so I wonder if there is some sophisticated way of doing it?
Here is some code (for popup menu):
QMenu *menuMode = new QMenu(this);
min = menu ->addAction("In");
mout = menu ->addAction("out");
ui->pushButtonMode->setMenu(menuMode); //I am writing in MainWindow, that's there is ui

This can be done by subclassing QMenu and moving the popup menu where you want to have it in showEvent:
popupmenu.h
#ifndef POPUPMENU_H
#define POPUPMENU_H
#include <QMenu>
class QPushButton;
class QWidget;
class PopupMenu : public QMenu
{
Q_OBJECT
public:
explicit PopupMenu(QPushButton* button, QWidget* parent = 0);
void showEvent(QShowEvent* event);
private:
QPushButton* b;
};
#endif // POPUPMENU_H
popupmenu.cpp
#include "popupmenu.h"
#include <QPushButton>
PopupMenu::PopupMenu(QPushButton* button, QWidget* parent) : QMenu(parent), b(button)
{
}
void PopupMenu::showEvent(QShowEvent* event)
{
QPoint p = this->pos();
QRect geo = b->geometry();
this->move(p.x()+geo.width()-this->geometry().width(), p.y());
}
mainwindow.cpp
...
PopupMenu* menu = new PopupMenu(ui->pushButton, this);
...
ui->pushButton->setMenu(menu);
It looks like this:

Another (imho) simpler approach would be:
void MainFrame::Slot_ShowMenu()
{
auto pMenu = new QMenu(this);
connect(pMenu, &QMenu::aboutToHide, pMenu, &QMenu::deleteLater);
...
// Retrieve a valid width of the menu. (It's not the same as using "pMenu->width()"!)
int menuWidth = pMenu->sizeHint().width();
int x = mUI.myQPushButton->width() - menuWidth;
int y = mUI.myQPushButton->height();
QPoint pos(mUI.myQPushButton->mapToGlobal(QPoint(x, y)));
pMenu->popup(pos);
}

You should implement an eventFilter for your QMenu. In the eventFilter method, you need to calculate the position where your menu will be shown.
Here you have an example:
.pro
TEMPLATE = app
QT += widgets
SOURCES += main.cpp \
dialog.cpp
HEADERS += dialog.h
FORMS += dialog.ui
main.cpp
#include <QtWidgets/QApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog dia;
return dia.exec();
}
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QtWidgets/QDialog>
#include <QMenu>
#include "ui_dialog.h"
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog();
protected:
bool eventFilter(QObject * obj, QEvent *event);
private:
QMenu *menu;
Ui::Dialog m_ui;
};
#endif
dialog.cpp
#include "dialog.h"
Dialog::Dialog()
{
m_ui.setupUi(this);
menu = new QMenu("menu", this);
menu->installEventFilter(this);
QAction *action = new QAction("action#1", this);
menu->addAction(action);
m_ui.pushButton->setMenu(menu);
}
bool Dialog::eventFilter(QObject * obj, QEvent *event)
{
if (event->type() == QEvent::Show && obj == m_ui.pushButton->menu())
{
int menu_x_pos = m_ui.pushButton->menu()->pos().x();
int menu_width = m_ui.pushButton->menu()->size().width();
int button_width = m_ui.pushButton->size().width();
QPoint pos = QPoint(menu_x_pos - menu_width + button_width,
m_ui.pushButton->menu()->pos().y());
m_ui.pushButton->menu()->move(pos);
return true;
}
return false;
}

Related

Show tooltip at mouse position and show legend on top-right corner

The following toy problem show two tabs, each tab contains a QGridLayout, which has a ScrollArea on one of the cells, which in turn contains a customized QLabel (MyLabel). When the user moves his mouse on the customzied QLabel, a tooltip shows up for several seconds.
test.pro
QT += core gui widgets
CONFIG += c++17
CONFIG += debug
QMAKE_CXXFLAGS += -std=c++17
SOURCES += \
test.cpp
QMAKE_CLEAN += $$TARGET Makefile
HEADERS += \
mywidget.h
test.cpp
#include "mywidget.h"
#include <QApplication>
#include <QtGui>
#include <QtCore>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyMainWindow myMainWindow;
myMainWindow.show();
return a.exec();
}
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QDebug>
#include <QWidget>
#include <QMdiArea>
#include <QMainWindow>
#include <QScrollArea>
#include <QLabel>
#include <QGridLayout>
#include <QMouseEvent>
#include <QToolTip>
class MyLabel : public QLabel
{
Q_OBJECT
public:
MyLabel(QWidget *parent=nullptr, const QString &name="")
: QLabel(parent), m_name(name)
{
resize(1800, 1200);
setMouseTracking(true);
}
private:
void mouseMoveEvent(QMouseEvent *ev) override {
QToolTip::showText(
ev->globalPosition().toPoint()
, m_name + ": " + QString::number(ev->pos().x()) + ", " + QString::number(ev->pos().y())
);
QLabel::mouseMoveEvent(ev);
}
private:
QString m_name;
};
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget* parent = nullptr, const QString &name="")
: QWidget(parent), m_name(name)
{
setWindowTitle(name);
m_gridLayout = new QGridLayout(this);
this->setLayout(m_gridLayout);
// layout col 1
m_gridLayout->addWidget(new QLabel("smaller label", this), 0, 0);
// layout col 2
m_scrollArea = new QScrollArea(this);
MyLabel *label = new MyLabel(this, m_name);
m_scrollArea->setWidget(label);
m_gridLayout->addWidget(m_scrollArea, 0, 1);
}
private:
QString m_name;
QGridLayout *m_gridLayout;
QScrollArea *m_scrollArea;
};
class MyMainWindow : public QMainWindow
{
Q_OBJECT
public:
MyMainWindow(QWidget* parent = nullptr)
: QMainWindow(parent)
{
m_mdiArea = new QMdiArea(this);
this->setCentralWidget(m_mdiArea);
MyWidget *myWidget1 = new MyWidget(this, "widget 1");
m_mdiArea->addSubWindow(myWidget1);
MyWidget *myWidget2 = new MyWidget(this, "widget 2");
m_mdiArea->addSubWindow(myWidget2);
m_mdiArea->setViewMode(QMdiArea::ViewMode::TabbedView);
}
private:
QMdiArea *m_mdiArea;
};
#endif // MYWIDGET_H
Here are two problems that I am struggling with:
How can I show the tooltip without moving my mouse when I toggle between those two tabs by Ctrl+Tab? In my real-world problem, I use the tooltip show information about data at the mouse point.
Is it possible show some legends on the top-right corner of the viewport of the QScollArea, regardless of the positions of the scoll bars? I am trying with paintEvent, but had difficulties get the position adjusting according to scoll bars.
Cursor postion can be retrieved by using QCursor::pos(), so both problems can be sovlved by using QCuros::pos() in paintEvent. I was confused by the fact paintEvent does not directly provide cursor position, as mouseMoveEvent does.

How to drag the QLabel from one window to another in Qt?

I am learning Qt for fun. And I got a question:
How could I drag and drop the QLabel in Qt among two different windows?
Here is what I have so far:
As you can tell from the .gif(which does not want to become downloaded and visible here for some reasons, but if you click on the link to it, you can clearly see it) provided above right now there are two main problems:
I can not move the QLabel outside of the window (and hence am not able to register the drag and drop event).
The label is flashing for some reasons when I am moving it.
Here is the relevant part of the implementation from the .gif:
#ifndef DRAGGERP_H
#define DRAGGERP_H
#include <QLabel>
#include <QApplication>
#include <QMouseEvent>
#include <QPoint>
class DraggerP : public QLabel
{
QPoint offset;
QPoint startingPosition;
public:
DraggerP(QWidget* parent = nullptr) : QLabel(parent){ }
protected:
void enterEvent(QEvent* event) override
{
QApplication::setOverrideCursor(Qt::PointingHandCursor);
}
void leaveEvent(QEvent* event) override
{
QApplication::restoreOverrideCursor();
}
void mousePressEvent(QMouseEvent* event) override
{
startingPosition = pos();
offset = QPoint(
event->pos().x() - pos().x() + 0.5*width(),
event->pos().y() - pos().y() + 0.5*height()
);
}
void mouseMoveEvent(QMouseEvent* event) override
{
move(event->pos() + offset);
}
void mouseReleaseEvent(QMouseEvent* event) override
{
move(startingPosition);
}
};
#endif // DRAGGERP_H
This is the extension of the QLabel I am using to create the drag and drop effect.
I do not need the whole solution, at least an idea of how to accomplish this and what am I doing wrong here.
Here is a pretty good example and I used it as a starting point.
That strange movement that the QLabel suffers is because the position of the QLabel now depends on the layout, the job of the layout is to establish the position of the widgets depending on the policies you establish.
The solution is not to implement those actions in the QLabel but in the MainWindow as I show below:
#include <QApplication>
#include <QLabel>
#include <QMainWindow>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QTime>
#include <QDrag>
#include <QMimeData>
#include <QMouseEvent>
class MainWindow: public QMainWindow {
QScrollArea scrollArea;
QWidget contentWidget;
QVBoxLayout lay;
public:
MainWindow(QWidget* parent=nullptr): QMainWindow(parent){
qsrand((uint) QTime::currentTime().msec());
setCentralWidget(&scrollArea);
scrollArea.setWidget(&contentWidget);
contentWidget.setLayout(&lay);
scrollArea.setWidgetResizable(true);
for(int i=0; i< 20; i++){
QLabel *label = new QLabel(QString("label %1").arg(i));
QPalette pal = label->palette();
pal.setColor(QPalette::Background, QColor(10 +qrand() % 240, 10 +qrand() % 240, 10 +qrand() % 240));
label->setAutoFillBackground(true);
label->setPalette(pal);
lay.addWidget(label);
}
setAcceptDrops(true);
}
protected:
void mousePressEvent(QMouseEvent *event){
QMainWindow::mousePressEvent(event);
QWidget *child = childAt(event->pos());
if(qobject_cast<QLabel *>(child))
createDrag(event->pos(), child);
}
void dropEvent(QDropEvent *event){
QByteArray byteArray = event->mimeData()->data("Label");
QWidget * widget = *reinterpret_cast<QWidget**>(byteArray.data());
QLabel * new_label = qobject_cast<QLabel *>(widget);
QWidget *current_children = childAt(event->pos());
QLabel * current_label = qobject_cast<QLabel*>(current_children);
int index = 0;
if(new_label){
if(current_label)
index = lay.indexOf(current_label);
else{
index = 0;
QLayoutItem *item = lay.itemAt(index);
while(item->widget()->pos().y() < event->pos().y() && item)
item = lay.itemAt(index++);
}
lay.insertWidget(index, new_label);
}
}
private:
void createDrag(const QPoint &pos, QWidget *widget){
if(widget == Q_NULLPTR)
return;
QByteArray byteArray(reinterpret_cast<char*>(&widget),sizeof(QWidget*));
QDrag *drag = new QDrag(this);
QMimeData * mimeData = new QMimeData;
mimeData->setData("Label",byteArray);
drag->setMimeData(mimeData);
QPoint globalPos = mapToGlobal(pos);
QPoint p = widget->mapFromGlobal(globalPos);
drag->setHotSpot(p);
drag->setPixmap(widget->grab());
drag->exec(Qt::CopyAction | Qt::MoveAction);
}
protected:
void dragEnterEvent(QDragEnterEvent *event){
if(event->mimeData()->hasFormat("Label"))
event->acceptProposedAction();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w1;
MainWindow w2;
w1.show();
w2.show();
return a.exec();
}

Create a QDockWidget that resizes to it's contents

I have an application where fixed-size child widgets need to be added programatically to a dock widget at run time based on user input. I want to add these widgets to a dock on the Qt::RightDockArea, from top to bottom until it runs out of space, then create a new column and repeat (essentially just the reverse of the flow layout example here, which I call a fluidGridLayout)
I can get the dock widget to resize itself properly using an event filter, but the resized dock's geometry doesn't change, and some of the widgets are drawn outside of the main window. Interestingly, resizing the main window, or floating and unfloating the dock cause it to 'pop' back into the right place (I haven't been able to find a way to replicate this programatically however)
I can't use any of the built-in QT layouts because with the widgets in my real program, they end up also getting drawn off screen.
Is there some way that I can get the dock to update it's top left coordinate to the proper position once it has been resized?
I think this may be of general interest as getting intuitive layout management behavior for dock widgets in QT is possibly the hardest thing known to man.
VISUAL EXMAPLE:
The code to replicate this is example given below.
Add 4 widgets to the program using the button
Resize the green bottom dock until only two widgets are shown. Notice that the 3 remaining widgets are getting painted outside the main window, however the dock is the right size, as evidenced by the fact that you can't see the close button anymore
Undock the blue dock widget. Notice it snaps to it's proper size.
Re-dock the blue dock to the right dock area. Notice it appears to be behaving properly now.
Now resize the green dock to it's minimum size. Notice the dock is now IN THE MIDDLE OF THE GUI. WTf, how is this possible??
THE CODE
Below I give the code to replicate the GUI from the screenshots.
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "QFluidGridLayout.h"
#include "QDockResizeEventFilter.h"
#include <QDockWidget>
#include <QGroupBox>
#include <QPushButton>
#include <QWidget>
#include <QDial>
class QTestWidget : public QGroupBox
{
public:
QTestWidget() : QGroupBox()
{
setFixedSize(50,50);
setStyleSheet("background-color: red;");
QDial* dial = new QDial;
dial->setFixedSize(40,40);
QLayout* testLayout = new QVBoxLayout;
testLayout->addWidget(dial);
//testLayout->setSizeConstraint(QLayout::SetMaximumSize);
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
setLayout(testLayout);
}
QSize sizeHint()
{
return minimumSize();
}
QDial* dial;
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QDockWidget* rightDock = new QDockWidget();
QDockWidget* bottomDock = new QDockWidget();
QGroupBox* central = new QGroupBox();
QGroupBox* widgetHolder = new QGroupBox();
QGroupBox* placeHolder = new QGroupBox();
placeHolder->setStyleSheet("background-color: green;");
placeHolder->setMinimumHeight(50);
widgetHolder->setStyleSheet("background-color: blue;");
widgetHolder->setMinimumWidth(50);
widgetHolder->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
widgetHolder->setLayout(new QFluidGridLayout);
widgetHolder->layout()->addWidget(new QTestWidget);
QPushButton* addWidgetButton = new QPushButton("Add another widget");
connect(addWidgetButton, &QPushButton::pressed, [=]()
{
widgetHolder->layout()->addWidget(new QTestWidget);
});
central->setLayout(new QVBoxLayout());
central->layout()->addWidget(addWidgetButton);
rightDock->setWidget(widgetHolder);
rightDock->installEventFilter(new QDockResizeEventFilter(widgetHolder,dynamic_cast<QFluidGridLayout*>(widgetHolder->layout())));
bottomDock->setWidget(placeHolder);
this->addDockWidget(Qt::RightDockWidgetArea, rightDock);
this->addDockWidget(Qt::BottomDockWidgetArea, bottomDock);
this->setCentralWidget(central);
central->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
this->setMinimumSize(500,500);
}
};
QFluidGirdLayout.h
#ifndef QFluidGridLayout_h__
#define QFluidGridLayout_h__
#include <QLayout>
#include <QGridLayout>
#include <QRect>
#include <QStyle>
#include <QWidgetItem>
class QFluidGridLayout : public QLayout
{
public:
enum Direction { LeftToRight, TopToBottom};
QFluidGridLayout(QWidget *parent = 0)
: QLayout(parent)
{
setContentsMargins(8,8,8,8);
setSizeConstraint(QLayout::SetMinAndMaxSize);
}
~QFluidGridLayout()
{
QLayoutItem *item;
while ((item = takeAt(0)))
delete item;
}
void addItem(QLayoutItem *item)
{
itemList.append(item);
}
Qt::Orientations expandingDirections() const
{
return 0;
}
bool hasHeightForWidth() const
{
return false;
}
int heightForWidth(int width) const
{
int height = doLayout(QRect(0, 0, width, 0), true, true);
return height;
}
bool hasWidthForHeight() const
{
return true;
}
int widthForHeight(int height) const
{
int width = doLayout(QRect(0, 0, 0, height), true, false);
return width;
}
int count() const
{
return itemList.size();
}
QLayoutItem *itemAt(int index) const
{
return itemList.value(index);
}
QSize minimumSize() const
{
QSize size;
QLayoutItem *item;
foreach (item, itemList)
size = size.expandedTo(item->minimumSize());
size += QSize(2*margin(), 2*margin());
return size;
}
void setGeometry(const QRect &rect)
{
QLayout::setGeometry(rect);
doLayout(rect);
}
QSize sizeHint() const
{
return minimumSize();
}
QLayoutItem *takeAt(int index)
{
if (index >= 0 && index < itemList.size())
return itemList.takeAt(index);
else
return 0;
}
private:
int doLayout(const QRect &rect, bool testOnly = false, bool width = false) const
{
int left, top, right, bottom;
getContentsMargins(&left, &top, &right, &bottom);
QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
int x = effectiveRect.x();
int y = effectiveRect.y();
int lineHeight = 0;
int lineWidth = 0;
QLayoutItem* item;
foreach(item,itemList)
{
QWidget* widget = item->widget();
if (y + item->sizeHint().height() > effectiveRect.bottom() && lineWidth > 0)
{
y = effectiveRect.y();
x += lineWidth + right;
lineWidth = 0;
}
if (!testOnly)
{
item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
}
y += item->sizeHint().height() + top;
lineHeight = qMax(lineHeight, item->sizeHint().height());
lineWidth = qMax(lineWidth, item->sizeHint().width());
}
if (width)
{
return y + lineHeight - rect.y() + bottom;
}
else
{
return x + lineWidth - rect.x() + right;
}
}
QList<QLayoutItem *> itemList;
Direction dir;
};
#endif // QFluidGridLayout_h__
QDockResizeEventFilter.h
#ifndef QDockResizeEventFilter_h__
#define QDockResizeEventFilter_h__
#include <QObject>
#include <QLayout>
#include <QEvent>
#include <QDockWidget>
#include <QResizeEvent>
#include "QFluidGridLayout.h"
class QDockResizeEventFilter : public QObject
{
public:
QDockResizeEventFilter(QWidget* dockChild, QFluidGridLayout* layout, QObject* parent = 0)
: QObject(parent), m_dockChild(dockChild), m_layout(layout)
{
}
protected:
bool eventFilter(QObject *p_obj, QEvent *p_event)
{
if (p_event->type() == QEvent::Resize)
{
QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(p_event);
QMainWindow* mainWindow = static_cast<QMainWindow*>(p_obj->parent());
QDockWidget* dock = static_cast<QDockWidget*>(p_obj);
// determine resize direction
if (resizeEvent->oldSize().height() != resizeEvent->size().height())
{
// vertical expansion
QSize fixedSize(m_layout->widthForHeight(m_dockChild->size().height()), m_dockChild->size().height());
if (dock->size().width() != fixedSize.width())
{
m_dockChild->resize(fixedSize);
m_dockChild->setFixedWidth(fixedSize.width());
dock->setFixedWidth(fixedSize.width());
mainWindow->repaint();
//dock->setGeometry(mainWindow->rect().right()-fixedSize.width(),dock->geometry().y(),fixedSize.width(), fixedSize.height());
}
}
if (resizeEvent->oldSize().width() != resizeEvent->size().width())
{
// horizontal expansion
m_dockChild->resize(m_layout->sizeHint().width(), m_dockChild->height());
}
}
return false;
}
private:
QWidget* m_dockChild;
QFluidGridLayout* m_layout;
};
#endif // QDockResizeEventFilter_h__
The problem is, nothing in the code above actually causes the QMainWindowLayout to recalculate itself. That function is buried within the QMainWindowLayout private class, but can be stimulated by adding and removing a dummy QDockWidget, which causes the layout to invalidate and recalcualte the dock widget positions
QDockWidget* dummy = new QDockWidget;
mainWindow->addDockWidget(Qt::TopDockWidgetArea, dummy);
mainWindow->removeDockWidget(dummy);
The only problem with this is that if you dig into the QT source code, you'll see that adding a dock widget causes the dock separator to be released, which causes unintuitive and choppy behavior as the user tries to resize the dock, and the mouse unexpectedly 'lets go'.
void QMainWindowLayout::addDockWidget(Qt::DockWidgetArea area,
QDockWidget *dockwidget,
Qt::Orientation orientation)
{
addChildWidget(dockwidget);
// If we are currently moving a separator, then we need to abort the move, since each
// time we move the mouse layoutState is replaced by savedState modified by the move.
if (!movingSeparator.isEmpty())
endSeparatorMove(movingSeparatorPos);
layoutState.dockAreaLayout.addDockWidget(toDockPos(area), dockwidget, orientation);
emit dockwidget->dockLocationChanged(area);
invalidate();
}
That can be corrected by moving the cursor back onto the separator and simulating a mouse press, basically undoing the endSeparatorMove callafter the docks have been repositioned. It's important to post the event, rather than send it, so thatit occurs after the resize event. The code for doing so looks like:
QPoint mousePos = mainWindow->mapFromGlobal(QCursor::pos());
mousePos.setY(dock->rect().bottom()+2);
QCursor::setPos(mainWindow->mapToGlobal(mousePos));
QMouseEvent* grabSeparatorEvent =
new QMouseEvent(QMouseEvent::MouseButtonPress,mousePos,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);
qApp->postEvent(mainWindow, grabSeparatorEvent);
Where 2 is a magic number that accounts for the group box border.
Put that all together, and here is the event filter than gives the desired behavior:
Corrected Event Filter
#ifndef QDockResizeEventFilter_h__
#define QDockResizeEventFilter_h__
#include <QObject>
#include <QLayout>
#include <QEvent>
#include <QDockWidget>
#include <QResizeEvent>
#include <QCoreApplication>
#include <QMouseEvent>
#include "QFluidGridLayout.h"
class QDockResizeEventFilter : public QObject
{
public:
friend QMainWindow;
friend QLayoutPrivate;
QDockResizeEventFilter(QWidget* dockChild, QFluidGridLayout* layout, QObject* parent = 0)
: QObject(parent), m_dockChild(dockChild), m_layout(layout)
{
}
protected:
bool eventFilter(QObject *p_obj, QEvent *p_event)
{
if (p_event->type() == QEvent::Resize)
{
QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(p_event);
QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(p_obj->parent());
QDockWidget* dock = static_cast<QDockWidget*>(p_obj);
// determine resize direction
if (resizeEvent->oldSize().height() != resizeEvent->size().height())
{
// vertical expansion
QSize fixedSize(m_layout->widthForHeight(m_dockChild->size().height()), m_dockChild->size().height());
if (dock->size().width() != fixedSize.width())
{
m_dockChild->setFixedWidth(fixedSize.width());
dock->setFixedWidth(fixedSize.width());
// cause mainWindow dock layout recalculation
QDockWidget* dummy = new QDockWidget;
mainWindow->addDockWidget(Qt::TopDockWidgetArea, dummy);
mainWindow->removeDockWidget(dummy);
// adding dock widgets causes the separator move event to end
// restart it by synthesizing a mouse press event
QPoint mousePos = mainWindow->mapFromGlobal(QCursor::pos());
mousePos.setY(dock->rect().bottom()+2);
QCursor::setPos(mainWindow->mapToGlobal(mousePos));
QMouseEvent* grabSeparatorEvent = new QMouseEvent(QMouseEvent::MouseButtonPress,mousePos,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);
qApp->postEvent(mainWindow, grabSeparatorEvent);
}
}
if (resizeEvent->oldSize().width() != resizeEvent->size().width())
{
// horizontal expansion
// ...
}
}
return false;
}
private:
QWidget* m_dockChild;
QFluidGridLayout* m_layout;
};
#endif // QDockResizeEventFilter_h__

QPushButton gets stuck drawing pressed when transfer of focus to button is interrupted

I have some forms with input fields that provide warnings to the user when the fields looses focus (if something is wrong of course). To that end I’ve created my own control that inherits from QLineEdit. For this question consider the extremely simple field that will pop a message box whenever focus is lost when there is text in the control. The actual controls have more logic but all are supposed to pop a message box. (Crossposted to qtcentre.org)
class test_line_edit : public QLineEdit
{
Q_OBJECT
public:
test_line_edit(QWidget * parent = 0) : QLineEdit(parent)
{}
~test_line_edit(){}
protected:
virtual void focusOutEvent(QFocusEvent * e)
{
if(!text().isEmpty())
{
QMessageBox msg;
msg.setText(tr("Test"));
msg.exec();
setFocus(Qt::OtherFocusReason);
}
else
{
QLineEdit::focusOutEvent(e);
}
}
};
Now say I have a simple form with just one of these controls and a QPushButton. If the user types in the control and then clicks the button the messagebox pops but the button gets stuck being drawn looking like it’s pressed. How can I make that stop? As best I can tell the QPushButton’s pressed signal is getting fired but that is all I can hear from the button. Since these test_line_edit controls are going to be used everywhere on many different forms I’m hoping there is something I can change inside the test_line_edit class that will fix the problem. Running Qt 4.7.0 on Windows XP using Visual Studio 2010.
I’ve seen this bug and not sure if it’s related since it’s a different platform and I’m not actually holding the button down: http://bugreports.qt-project.org/browse/QTBUG-7901
Here’s a full program that demonstrates the problem (warning: some code generated from the Qt VS plugin)
main.h:
#include <QtGui/QMainWindow>
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>
#include <QLineEdit>
#include <QMessageBox>
class test_line_edit : public QLineEdit
{
Q_OBJECT
public:
test_line_edit(QWidget * parent = 0) : QLineEdit(parent)
{}
~test_line_edit(){}
protected:
virtual void focusOutEvent(QFocusEvent * e)
{
if(!text().isEmpty())
{
QMessageBox msg;
msg.setText(tr("Test"));
msg.exec();
//setFocus(Qt::OtherFocusReason);
}
else
{
QLineEdit::focusOutEvent(e);
}
QLineEdit::focusOutEvent(e);
}
};
QT_BEGIN_NAMESPACE
class Ui_btn_drawing_testClass
{
public:
QWidget *centralWidget;
QVBoxLayout *verticalLayout;
test_line_edit *line_edit;
QPushButton *btn;
QMenuBar *menuBar;
QToolBar *mainToolBar;
QStatusBar *statusBar;
void setupUi(QMainWindow *btn_drawing_testClass)
{
if (btn_drawing_testClass->objectName().isEmpty())
btn_drawing_testClass->setObjectName(QString::fromUtf8("btn_drawing_testClass"));
btn_drawing_testClass->resize(167, 127);
centralWidget = new QWidget(btn_drawing_testClass);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
verticalLayout = new QVBoxLayout(centralWidget);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
line_edit = new test_line_edit(centralWidget);
line_edit->setObjectName(QString::fromUtf8("line_edit"));
verticalLayout->addWidget(line_edit);
btn = new QPushButton(centralWidget);
btn->setObjectName(QString::fromUtf8("btn"));
verticalLayout->addWidget(btn);
btn_drawing_testClass->setCentralWidget(centralWidget);
menuBar = new QMenuBar(btn_drawing_testClass);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
menuBar->setGeometry(QRect(0, 0, 167, 20));
btn_drawing_testClass->setMenuBar(menuBar);
mainToolBar = new QToolBar(btn_drawing_testClass);
mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
btn_drawing_testClass->addToolBar(Qt::TopToolBarArea, mainToolBar);
statusBar = new QStatusBar(btn_drawing_testClass);
statusBar->setObjectName(QString::fromUtf8("statusBar"));
btn_drawing_testClass->setStatusBar(statusBar);
retranslateUi(btn_drawing_testClass);
QMetaObject::connectSlotsByName(btn_drawing_testClass);
} // setupUi
void retranslateUi(QMainWindow *btn_drawing_testClass)
{
btn_drawing_testClass->setWindowTitle(QApplication::translate("btn_drawing_testClass", "btn_drawing_test", 0, QApplication::UnicodeUTF8));
btn->setText(QApplication::translate("btn_drawing_testClass", "PushButton", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class btn_drawing_testClass: public Ui_btn_drawing_testClass {};
} // namespace Ui
QT_END_NAMESPACE
class btn_drawing_test : public QMainWindow
{
Q_OBJECT
public:
btn_drawing_test(QWidget *parent = 0, Qt::WFlags flags = 0): QMainWindow(parent, flags)
{
ui.setupUi(this);
}
~btn_drawing_test(){}
private:
Ui::btn_drawing_testClass ui;
protected slots:
void on_btn_clicked()
{
int breakpoint = 5;
}
void on_btn_pressed()
{
int breakpoint = 5;
}
void on_btn_toggled(bool)
{
int breakpoint = 5;
}
};
main.cpp:
#include <QtGui/QApplication>
#include "main.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
btn_drawing_test w;
w.show();
return a.exec();
}
Thank you for your help and please let me know if I can provide any more information.
I've made some changes to your line edit class, see if it solves the problem:
class test_line_edit : public QLineEdit
{
Q_OBJECT
public:
test_line_edit(QWidget * parent = 0) : QLineEdit(parent) {}
~test_line_edit(){}
protected:
virtual void focusOutEvent(QFocusEvent * e)
{
QLineEdit::focusOutEvent(e);
QEvent *event = new QEvent(QEvent::User);
QApplication::postEvent(this, event);
}
virtual void customEvent(QEvent * event)
{
QLineEdit::customEvent(event);
if (event->type()==QEvent::User)
{
QMessageBox msg;
msg.setText(tr("Test"));
msg.exec();
setFocus(Qt::OtherFocusReason);
}
}
};
Instead of showing modal window in the focusOutEvent method, I've posted an event into the event queue. This worked fine on my ubuntu, don't expect any problems on windows either.
hope this helps, regards

Qt - QStackedWidget and QTabsExample help

Basically, I have multiple widgets I'm trying to switch between... and the default is a QTabWidget. Aside from some modification, the two examples (QStackedWidget and QTabsExapmle) are just mingled together. I can't get the "connect" portion to work (get an error: no matching function for call to QTabsExample::connect), and nothing displays on screen unless stackedWidget->addWidget(tabWidget) is the first in the list (and even then, I only see the view in the upper left hand corner).
QTabsExample.h
#ifndef QTABSEXAMPLE_H
#define QTABSEXAMPLE_H
#include <QtGui/QMainWindow>
#include <QtGui/QScrollArea>
#include <QtGui/QFrame>
#include <QtGui/QVBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QGroupBox>
#include <QtGui/QFormLayout>
#include <QtGui/QMessageBox>
#include <QtCore/QPointer>
#include <QtCore/QFile>
#include <QtCore/QIODevice>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtXml/QXmlStreamReader>
#include <QtDebug>
#include <QBool>
#include <QSignalMapper>
#include <QStackedLayout>
class QTabsExample: public QMainWindow {
Q_OBJECT
public:
QTabsExample(QWidget *parent = 0);
~QTabsExample();
private:
void buildTabMenuBar(int index);
public slots:
void activeTabChanged(int index);
void setCurrentIndex(int);
signals:
void activated(int);
private:
QTabWidget* tabWidget;
QWidget* customWidget1;
QWidget* customWidget2;
QWidget* customWidget3;
};
// ---------------------------------------------------------------------------
// QMyWidget - Tab1
//
class QMyWidget: public QWidget {
Q_OBJECT
public:
QMyWidget(QWidget *parent = 0);
~QMyWidget();
public slots:
void runOnTabSelect();
private:
QPointer<QVBoxLayout> _layout;
};
// ---------------------------------------------------------------------------
// QMyWidget2 - Tab2
//
class QMyWidget2: public QWidget {
Q_OBJECT
public:
QMyWidget2(QWidget *parent = 0);
~QMyWidget2();
public slots:
void runOnTabSelect();
};
// ---------------------------------------------------------------------------
// QMyWidget3 - Tab3
//
class QMyWidget3: public QWidget {
Q_OBJECT
public:
QMyWidget3(QWidget *parent = 0);
~QMyWidget3();
public slots:
void runOnTabSelect();
private:
QPointer<QVBoxLayout> _layout;
};
#endif // QTABSEXAMPLE_H
QTabsExample.cpp
#include <QtGui>
#include <QApplication>
#include "QTabsExample.h"
QTabsExample::QTabsExample(QWidget *parent) : QMainWindow(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
this->setWindowTitle("Main Window, I think");
QStackedLayout *stackedLayout = new QStackedLayout;
//create tab widget
QTabWidget *tabWidget = new QTabWidget();
tabWidget->setContextMenuPolicy(Qt::NoContextMenu);
QObject::connect(tabWidget, SIGNAL(currentChanged(int)),this, SLOT(activeTabChanged(int)));
QMyWidget* widget1 = new QMyWidget();
tabWidget->addTab(widget1, "Tab1");
QMyWidget2* widget2 = new QMyWidget2();
tabWidget->addTab(widget2, "Tab2");
QMyWidget3* widget3 = new QMyWidget3();
tabWidget->addTab(widget3, "Tab3");
//set programatically
tabWidget->setStyleSheet("QTabBar::tab { height: 70px; width: 80px; font-size: 15px;}");
//create other widgets
QWidget* customWidget1 = new QWidget;
QWidget* customWidget2 = new QWidget;
QWidget* customWidget3 = new QWidget;
//add layouts to widgets
customWidget1->setContextMenuPolicy(Qt::NoContextMenu);
customWidget2->setContextMenuPolicy(Qt::NoContextMenu);
customWidget3->setContextMenuPolicy(Qt::NoContextMenu);
customWidget1->setWindowTitle("Widget 1");
customWidget2->setWindowTitle("Widget 2");
customWidget3->setWindowTitle("Widget 3");
//insert content to make sure it's viewable
QPalette palette;
palette.setBrush(this->backgroundRole(), QBrush(QImage("c://default.png")));
customWidget1->setPalette(palette);
palette.setBrush(this->backgroundRole(), QBrush(QImage("c://default2.png")));
customWidget2->setPalette(palette);
palette.setBrush(this->backgroundRole(), QBrush(QImage("c://default3.png")));
customWidget3->setPalette(palette);
//add widgets to stack
stackedLayout->addWidget(tabWidget);
stackedLayout->addWidget(customWidget1);
stackedLayout->addWidget(customWidget2);
stackedLayout->addWidget(customWidget3);
QComboBox *pageComboBox = new QComboBox;
pageComboBox->addItem(tr("Tab Page"));
pageComboBox->addItem(tr("page 2"));
pageComboBox->addItem(tr("page 3"));
pageComboBox->addItem(tr("page 4"));
connect(pageComboBox, SIGNAL(activated(int))), stackedLayout, SLOT(setCurrentIndex(int));
QVBoxLayout *_layout = new QVBoxLayout;
_layout->addWidget(pageComboBox);
_layout->addLayout(stackedLayout);
setLayout(_layout);
//setCentralWidget(tabWidget);
#ifdef Q_OS_SYMBIAN
QWidgetList widgets = QApplication::allWidgets();
QWidget* w = 0;
foreach(w,widgets) {
w->setContextMenuPolicy(Qt::NoContextMenu);
}
#endif
}
QTabsExample::~QTabsExample(){
}
void QTabsExample::activeTabChanged(int index) {
buildTabMenuBar(index);
}
void QTabsExample::buildTabMenuBar(int index) {
QMenuBar* menubar = menuBar();
menubar->clear();
switch (index) {
case 0:
{
menubar->addAction("", tabWidget->widget(index), SLOT(runOnTabSelect()));
break;
}
case 1:
{
menubar->addAction("", tabWidget->widget(index), SLOT(runOnTabSelect()));
break;
}
case 2:
{
menubar->addAction("", tabWidget->widget(index), SLOT(runOnTabSelect()));
break;
}
default:
{
break;
}
};
}
//tab1
QMyWidget::QMyWidget(QWidget *parent) : QWidget(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
QVBoxLayout* _layout = new QVBoxLayout(this);
//buttons get created
this->setLayout(_layout);
}
QMyWidget::~QMyWidget() {
}
void QMyWidget::runOnTabSelect() {
}
//tab2
QMyWidget2::QMyWidget2(QWidget *parent) : QWidget(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
QVBoxLayout* _layout = new QVBoxLayout(this);
//buttons get created
this->setLayout(_layout);
}
QMyWidget2::~QMyWidget2() {
}
void QMyWidget2::runOnTabSelect() {
}
//tab3
QMyWidget3::QMyWidget3(QWidget *parent) : QWidget(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
QVBoxLayout* _layout = new QVBoxLayout(this);
//buttons get created
this->setLayout(_layout);
}
QMyWidget3::~QMyWidget3() {
}
void QMyWidget3::runOnTabSelect() {
}
main.cpp
#include "QTabsExample.h""
#include <QtGui>
#include <QApplication>
#include <QtGui/QApplication>
#include <QPixmap>
#include <QWidget>
#include <QMainWindow>
#include <QSplashScreen>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTabsExample w;
w.showMaximized();
return a.exec();
}
Well for starters you need to uncomment //QTabsExample w; in main.cpp, but I guess, that is just a relict from experimenting...
Also, what do you mean by "nothing displays on screen."?
Do you mean, that an empty window pops up? Or does no window at all open? Or does the screen turn black?
Have you eliminated the possibility that the "app.css" file might be corrupt?
Have you tried deleting the Makefiles and moc_* files?
EDIT:
I tried to simplify the constructor of QTabsExample
#include <QtGui>
#include <QApplication>
#include "QTabsExample.h"
QTabsExample::QTabsExample(QWidget *parent) : QMainWindow(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
this->setWindowTitle("Main Window, I think");
//create tab widget
QTabWidget *tabWidget = new QTabWidget();
tabWidget->setContextMenuPolicy(Qt::NoContextMenu);
//QObject::connect(tabWidget, SIGNAL(currentChanged(int)),this, SLOT(activeTabChanged(int))); // I have no experience with mobile developement
// can you manipulate the menubar on a mobile device?
QMyWidget* widget1 = new QLabel(tr("Widget1")); // simplification
tabWidget->addTab(widget1, "Tab1");
QMyWidget2* widget2 = new QLabel(tr("Widget2"));
tabWidget->addTab(widget2, "Tab2");
QMyWidget3* widget3 = new QLabel(tr("Widget3"));
tabWidget->addTab(widget3, "Tab3");
//set programatically
tabWidget->setStyleSheet("QTabBar::tab { height: 70px; width: 80px; font-size: 15px;}");
//create other widgets
QWidget* customWidget1 = new QLabel(tr("Hello1")); // simplification
QWidget* customWidget2 = new QLabel(tr("Hello2"));
QWidget* customWidget3 = new QLabel(tr("Hello3"));
// create stacked layout (closer to where it is actually used)
QStackedLayout *stackedLayout = new QStackedLayout;
//add widgets to stack
stackedLayout->addWidget(tabWidget);
stackedLayout->addWidget(customWidget1);
stackedLayout->addWidget(customWidget2);
stackedLayout->addWidget(customWidget3);
QComboBox *pageComboBox = new QComboBox;
pageComboBox->addItem(tr("Tab Page"));
pageComboBox->addItem(tr("page 2"));
pageComboBox->addItem(tr("page 3"));
pageComboBox->addItem(tr("page 4"));
connect(pageComboBox, SIGNAL(activated(int))), stackedLayout, SLOT(setCurrentIndex(int));
QVBoxLayout *_layout = new QVBoxLayout;
_layout->addWidget(pageComboBox);
_layout->addLayout(stackedLayout);
setLayout(_layout);
//setCentralWidget(tabWidget);
#ifdef Q_OS_SYMBIAN
QWidgetList widgets = QApplication::allWidgets();
QWidget* w = 0;
foreach(w,widgets) {
w->setContextMenuPolicy(Qt::NoContextMenu);
}
#endif
}
Expected behaviour:
a combo box on the top with choices: "Tab Page", "page 2", ...
below a QTabWidget witht the tabs: "Tab1", "Tab2", "Tab3"
Tab1 should be displayed with a QLabel, which says "Widget1"
when you select another tab, the label should say "WidgetX" // depending on your choice
when you select another widget from the combo box you should see "HelloX" // depending on your choice