Qt paintEvent Can I paint on a child widget? - qt

I have a widget class subclass of QMainWindow, and it has a central widget(QWidget), and in the overriden paintEvent function, can I create an instance of QPainter on this central widget? Code like:
void MyMainWindow::paintEvent(QEvent *event)
{
QPainter painter(_theCentralWidget);
//drawing...
return QMainWindow::paintEvent(event);
}
I don't want to create a new c++ class subclass of QWidget and override its paintEvent function and then replace the original central widget with this new one...
(I did like above but an error occured saying the painter is not active...)

Well. If you really, really, really don't want to sub-class the central widget, you can install a event filter to it and handle the paint event for it.
http://qt-project.org/doc/qt-4.8/qobject.html#installEventFilter

You can use C++11 lambdas to solve this problem. Create a new generic "event for anything" QObject that passes its filtering through a lambda you specify. Then add this generic object to your display widget with the desired logic. For example:
generic-qevent-filter.hpp:
#pragma once
class GenericQEventFilter : public QObject
{
Q_OBJECT
public:
GenericQEventFilter(QObject *parent, std::function<bool (QObject *obj, QEvent *event)> event_filter_f);
std::function<bool (QObject *obj, QEvent *event)> event_filter_f;
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
generic-qevent-filter.cpp:
#include "generic-qevent-filter.hpp"
GenericQEventFilter::GenericQEventFilter(QObject *parent,
std::function<bool (QObject *obj, QEvent *event)> event_filter_f)
: QObject(parent), event_filter_f(event_filter_f)
{
}
bool GenericQEventFilter::eventFilter(QObject *obj, QEvent *event)
{
return this->event_filter_f(obj, event);
}
And you use this in your code as follows:
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
{
ui = new Ui_MainWindow();
ui->setupUi(this); // Initialise widgets
this->wire_up_gui(); // Connect signals and slots
ui->displayWidget->installEventFilter(new GenericQEventFilter(this, [&] (QObject *obj, QEvent *event) {
if(event->type() == QEvent::Paint) {
paint_display_widget(obj, event);
return true;
}
return false;
}));
}

"Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting."
From: QT Docu

Related

Qt window resize aspect ratio 1:1

I want to resize the app window proportionally 1:1. I tried to change it inside the ResizeEvent, but then I got the window flickering. Now my code looks like this, but it doesn't work.
filterobject.h:
class FilterObject:public QObject{
public:
QWidget *target = nullptr;//it holds a pointer to target object
int goalHeight=0;
FilterObject(QObject *parent=nullptr):QObject(parent){}//uses QObject constructor
bool eventFilter(QObject *watched, QEvent *event) override;//and overrides eventFilter function
};
widget.h:
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
virtual int heightForWidth ( int w ) const { return w*9/16;}
//virtual void resizeEvent(QResizeEvent *event) override;
~Widget();
private:
Ui::Widget *ui;
};
widget.cpp:
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void QWidget::resizeEvent(QResizeEvent *event){
FilterObject *filter = new FilterObject();
QWidget *targetWidget = new QWidget();
filter->target=targetWidget;
targetWidget->installEventFilter(filter);
}
bool FilterObject::eventFilter(QObject *watched, QEvent *event) {
if(watched != target){//checks for correct target object.
return false;
}
if(event->type() != QEvent::Resize){//and correct event
return false;
}
QResizeEvent *resEvent = static_cast<QResizeEvent*>(event);
goalHeight = 9*resEvent->size().width()/16;
if(target->height()!=goalHeight){
target->setFixedHeight(goalHeight);
}
return true;
};
Perhaps this code will work, but my condition if(event->type() != QEvent::Resize) does not work .. Any ideas?
You have some problems in your code. First of all you should install event filter once e.g in your constructor. You create an object of event filter and install it every time resizeEvent is triggered which is wrong. Also you are installing event filter on the wrong object (a new QWidget). So remove the resizeEvent function and insert in the constructor of Widget:
FilterObject *filter = new FilterObject();
filter->target=this;
installEventFilter(filter);

QLabel click event using Qt?

I'm new in Qt and have a question.
I have QLabel and QLineEdit objects, and when QLabel text is clicked on, I want to set this text in QLineEdit.
Also I have read that QLabel has not clicked signal.
Can you explain how can I do this and write code for me ?!
Either style another type of QWidget such as a specific QPushButton to look like a QLabel and use its clicked() signal or inherit QLabel yourself and emit your own clicked() signal.
See this example:
https://wiki.qt.io/Clickable_QLabel
If you choose the latter option you can pass the text in the signal. Then connect the necessary signals/slots up between the QLabel and the QLineEdit like so:
QObject::connect(&label, SIGNAL(clicked(const QString& text)),
&lineEdit, SLOT(setText(const QString& text)));
A simple way to accomplish that, without a need for any subclassing, is a signal source that monitors the events on some object and emits relevant signals:
// main.cpp - this is a single-file example
#include <QtWidgets>
class MouseButtonSignaler : public QObject {
Q_OBJECT
bool eventFilter(QObject * obj, QEvent * ev) Q_DECL_OVERRIDE {
if ((ev->type() == QEvent::MouseButtonPress
|| ev->type() == QEvent::MouseButtonRelease
|| ev->type() == QEvent::MouseButtonDblClick)
&& obj->isWidgetType())
emit mouseButtonEvent(static_cast<QWidget*>(obj),
static_cast<QMouseEvent*>(ev));
return false;
}
public:
Q_SIGNAL void mouseButtonEvent(QWidget *, QMouseEvent *);
MouseButtonSignaler(QObject * parent = 0) : QObject(parent) {}
void installOn(QWidget * widget) {
widget->installEventFilter(this);
}
};
The emit keyword is an empty macro, Qt defines it as follows:
#define emit
It is for use by humans as a documentation aid prefix only, the compiler and moc ignore it. As a documentation aid, it means: the following method call is a signal emission. The signals are simply methods whose implementation is generated for you by moc - that's why we have to #include "main.moc" below to include all the implementations that moc has generated for the object class(es) in this file. There's otherwise nothing special or magical to a signal. In this example, you could look in the build folder for a file called main.moc and see the implementation (definition) of void MouseButtonSignaler::mouseButtonEvent( .. ).
You can then install such a signaler on any number of widgets, such as a QLabel:
int main(int argc, char ** argv) {
QApplication app(argc, argv);
MouseButtonSignaler signaler;
QWidget w;
QVBoxLayout layout(&w);
QLabel label("text");
QLineEdit edit;
layout.addWidget(&label);
layout.addWidget(&edit);
signaler.installOn(&label);
QObject::connect(&signaler, &MouseButtonSignaler::mouseButtonEvent,
[&label, &edit](QWidget*, QMouseEvent * event) {
if (event->type() == QEvent::MouseButtonPress)
edit.setText(label.text());
});
w.show();
return app.exec();
}
#include "main.moc"
You need to create one Custom Label class, which will inherit QLabel. Then you can use MouseButtonRelease event to check clicking of Label and emit your custom signal and catch in one SLOT.
Your .h file will be as below:
class YourLabelClass : public QLabel{
signals:
void myLabelClicked(); // Signal to emit
public slots:
void slotLabelClicked(); // Slot which will consume signal
protected:
bool event(QEvent *myEvent); // This method will give all kind of events on Label Widget
};
In your .cpp file, your constructor will connect signal & slot as below :
YourLabelClass :: YourLabelClass(QWidget* parent) : QLabel(parent) {
connect(this, SIGNAL(myLabelClicked()), this, SLOT(slotLabelClicked()));
}
Remaining event method and SLOT method will be implemented as below:
bool YourLabelClass :: event(QEvent *myEvent)
{
switch(myEvent->type())
{
case(QEvent :: MouseButtonRelease): // Identify Mouse press Event
{
qDebug() << "Got Mouse Event";
emit myLabelClicked();
break;
}
}
return QWidget::event(myEvent);
}
void YourLabelClass :: slotLabelClicked() // Implementation of Slot which will consume signal
{
qDebug() << "Clicked Label";
}
For Changing a Text on QLineEdit, you need to create a Custom Class and share object pointer with custom QLabel Class. Please check test code at this link
In the above example the header needs Q_OBJECT:
class YourLabelClass : public QLabel{
Q_OBJECT
signals:

Qt rightclick QPushButton

I'm using Qt Creator to create a gui for a mineseeper game.
How can I know a QpushButton clicked with rightclick? for flag in the game.
In other word, which signal used for rightclick?
Create your own button with filter at mousePressEvent slot.
qrightclickbutton.h
#ifndef QRIGHTCLICKBUTTON_H
#define QRIGHTCLICKBUTTON_H
#include <QPushButton>
#include <QMouseEvent>
class QRightClickButton : public QPushButton
{
Q_OBJECT
public:
explicit QRightClickButton(QWidget *parent = 0);
private slots:
void mousePressEvent(QMouseEvent *e);
signals:
void rightClicked();
public slots:
};
#endif // QRIGHTCLICKBUTTON_H
qrightclickbutton.cpp
#include "qrightclickbutton.h"
QRightClickButton::QRightClickButton(QWidget *parent) :
QPushButton(parent)
{
}
void QRightClickButton::mousePressEvent(QMouseEvent *e)
{
if(e->button()==Qt::RightButton)
emit rightClicked();
}
Now connect like this
QRightClickButton *button = new QRightClickButton(this);
ui->gridLayout->addWidget(button);
connect(button, SIGNAL(rightClicked()), this, SLOT(onRightClicked()));
Create a slot in MainWindow.cpp.
void MainWindow::onRightClicked()
{
qDebug() << "User right clicked me";
}
It works for me!
I think QPushButton is internally implemented to listen to left mouse clicks only. But you can easily extend QPushButton and re-implement let's say the mouse release event and do your thing if the right mouse button was pressed, e.g. emit a custom rightClicked() signal for example:
signals:
void rightClicked();
protected:
void mouseReleaseEvent(QMouseEvent *e) {
if (e->button() == Qt::RightButton) emit rightClicked();
else if (e->button() == Qt::LeftButton) emit clicked();
}
... or you can create an overload of the clicked signal that forwards the mouseEvent pointer so you can do the same check outside of the button.
signals:
void clicked(QMouseEvent *);
protected:
void mouseReleaseEvent(QMouseEvent *e) {
emit clicked(e);
}
Then you do the check in the slot you connect the button's clicked(QMouseEvent *) signal to and proceed accordingly.
I just wrote this little helper adapter to make any existing button right-clickable with no need to subclass it:
class CRightClickEnabler : public QObject
{
public:
CRightClickEnabler(QAbstractButton * button): QObject(button), _button(button) {
button->installEventFilter(this);
};
protected:
inline bool eventFilter(QObject *watched, QEvent *event) override {
if (event->type() == QEvent::MouseButtonPress)
{
auto mouseEvent = (QMouseEvent*)event;
if (mouseEvent->button() == Qt::RightButton)
_button->click();
}
return false;
}
private:
QAbstractButton* _button;
};
Usage:
connect(ui->pushButton, &QPushButton::clicked, [](){qDebug() << "Button clicked";});
new CRightClickEnabler(ui->pushButton);
From now on, the clicked signal will be triggered by the right click as well as left click. There's no need to delete this object - it uses ui->pushButton as parent and will be auto-deleted by Qt when the parent is destroyed.
Obviously, you can write 2 lines of code (literally) to declare a new signal here and emit that signal upon right click instead of clicked, if desired.
I'd like to suggest this option as well, without need for event filter/other stuffs...
self.button.released.connect(self.doStuff)
self.button.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.button.customContextMenuRequested.connect(partial(self.doStuff, False))
def doStuff(self,state=True,p=QPoint()):
print("True for left, False for right!",state)

Qt resize event for docks

Is there a way to catch resize events for docks in Qt? I need to be able to detect when a dock is resized (and not only when its location or 'features' change). It looks like there is no 'resized' signal for QDockWidget.
If you do not wish to subclass to just get resize event control, you can installEventFilter
Small example would look like (MainWindow.h): (MainWindow holds DockWidget Here)
protected:
bool eventFilter(QObject *obj, QEvent *event);
and in (MainWindow.cc):
MainWindow::MainWindow(QWidget* parent)
: QWidget(parent) {
ui_.setupUi(this);
ui_.dockWidget->installEventFilter(this);
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
if (event->type() == QEvent::Resize && obj == ui_.dockWidget) {
QResizeEvent *resizeEvent = static_cast<QResizeEvent*>(event);
qDebug("Dock Resized (New Size) - Width: %d Height: %d",
resizeEvent->size().width(),
resizeEvent->size().height());
}
return QWidget::eventFilter(obj, event);
}

QGraphicsview mouserelease event does not register

I am trying to get the scene coordinates of a both a mousepress and a mouserelease event by means of an event filter.
It works fine for the mousepress, but the mouserelease seems not to be registering.
What am I missing here?
Please note I am using Qt Creator 2.2.0(based on Qt 4.7.4)
class mouseEater : public QObject{
// Q_OBJECT
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mouseEater *eater = new mouseEater();
QGraphicsScene *scene;
scene = new QGraphicsScene();
ui->view->installEventFilter(eater);
ui->view->setScene(scene);
}
bool mouseEater::eventFilter(QObject *obj, QEvent *event){
QMouseEvent *mEvent = static_cast<QMouseEvent *>(event);
QPoint point = mEvent->pos();
cout<<"type:"<<event->type()<<endl;
switch(event->type()){
case QEvent::MouseButtonPress:
cout<<"mousepress at "<<point.x()<<","<<point.y()<<endl;
return true;
break;
case QEvent::MouseButtonRelease:
cout<<"mouserelease at "<<point.x()<<","<<point.y()<<endl;
return true;
break;
default:
//standard event processing
return QObject::eventFilter(obj, event);
break;
}
return 0;
}
The quick answer is that QGraphicsView doesn't seem to allow other QObjects to eat its mouseReleaseEvents. I'm not sure why just yet, but I think it's something to do with how mouseReleaseEvent() is reimplemented in QGraphicsView. A little hacking gives a workaround though:
HEADER:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
virtual bool eventFilter(QObject *, QEvent *);
private:
Ui::MainWindow *ui;
};
class cDerived : public QGraphicsView
{
Q_OBJECT
public:
cDerived(QWidget *parent) : QGraphicsView(parent) {}
protected:
virtual void mousePressEvent(QMouseEvent *event);
virtual void mouseReleaseEvent(QMouseEvent *event);
};
SOURCE:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsView *View = new cDerived(ui->centralWidget);
QGraphicsScene *Scene = new QGraphicsScene(ui->centralWidget->rect(), ui->centralWidget);
View->setScene(Scene);
View->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
switch(event->type())
{
case QEvent::MouseButtonPress:
qDebug()<<"eaten mousepress";
return true;
break;
case QEvent::MouseButtonRelease:
qDebug()<<"eaten mouserelease";
return true;
break;
default:
//standard event processing
return QObject::eventFilter(obj, event);
break;
}
}
void cDerived::mousePressEvent(QMouseEvent *event)
{
qDebug()<<"mousepress";
event->ignore();
}
void cDerived::mouseReleaseEvent(QMouseEvent *event)
{
qDebug()<<"mouserelease";
event->ignore();
}
Try commenting out the 'event->ignore()' calls in the reimplemented mouse event catchers and it becomes clear how the mouseReleaseEvent() in QGraphicsView might be going wrong...
If you can reimplement QGraphicsView as a very thin class with just the constructor and mouseReleaseEvent() { event->ignore() } then you should be able to get around this bug (though, while this hack seems to be stable - suggesting that your problem might be a bug in QGraphicsView, without exhaustively looking through the QGraphicsView source we can't be sure this isn't in fact an intended feature and therefore that this hack won't break anything down the line!)
-Sam
Use QGraphicsSceneMouseEvent and the likes instead of QMouseEvent. As I found out in my question, QGraphicsScene does not dispatch normal mouse events.
Best regards

Resources