Reimplemented resizeEvent() is not called - c++

Please check the following implementation:
BoardView.h
#include <QGraphicsView>
class BoardView : public QGraphicsView
{
protected:
void resizeEvent(QResizeEvent* event);
public:
BoardView();
};
BoardView.cpp
BoardView::BoardView()
{
}
void BoardView::resizeEvent(QResizeEvent* event)
{
QGraphicsView::resizeEvent(event);
double wd = width();
double ht = height();
double min_wh = min(wd, ht);
qDebug() << min_wh;
QTransform transform;
transform.scale(min_wh / Options::getMainBoardMinSize(), min_wh / Options::getMainBoardMinSize());
setTransform(transform);
}
The above two files are directly copied from an existing project, say P1. The purpose of this code is to handle resizeEvent() and scale the view according to widget size.
The problem is, this works exactly as it should in project P1. Everything is scaled as expected. But resizeEvent() is not called from the new project at all, when the view is resized by resizing main window, on which it is a widget.
The BoardView object does everything else as expected. It shows the scene. It captures and passes mouse events to the scene etc. Only the resizeEvent() is not called.
What am I doing wrong?
I'm using Qt Creator 2.0.1, Qt 4.7 32 bit on Windows 7 Ultimate 32 bit.

Hmm... Are you missing some code here?
Or your class declaration is incorrect - you should change it to
class BoardView : public QGraphicsView
{
Q_OBJECT
...
public:
BoardView(QWidget* parent);
};
...
BoardView::BoardView(QWidget* parent):QGraphicsView(parent)
{
}
Sorry if this is omitted by you because of triviality, but I don't know your QT experience level. This might be why QT events works incorrectly with your class...

Why a BoardView::resizeEvent() is not called, yet the QGraphicsView was showing the QGraphicsScene correctly? The reason is a silly mistake, instead of
BoardView _board_view;
I wrote
QGraphicsView _board_view;
Sorry for posting such a silly question!

Related

How to expand tabs in QTabWidget Qt

I have a QTabWidget like this one:
But I want to expand the tabs to "fill" the entire widget width, like this:
How can I do that?
I am using Qt 5.3.2 and Qt Creator 3.2.1
Update:
I tried to use the setExpanding function:
ui->myTabWidget->tabBar()->setExpanding(true);
But it didn't work.
I found that QTabBar has a setExpanding method, which appears to do exactly what you want, but I tried it (on Windows), and it doesn't work. This is the code:
ui->tabWidget->tabBar()->setExpanding (true);
Then I found the following post:
https://forum.qt.io/topic/47404/qtabbar-will-not-expand-its-tabs
I find the answer provided in the above post to be debatable. He says it's respecting the operating system style whether or not the expanding property is set to true and that it's a feature, not a bug, and that you have to subclass QTabBar to get the desired behavior. If I write code to do a specific thing, I feel like my instructions should override the OS style. If I just wanted the OS style, I would leave that special code out. However much I disagree with the implementation, that appears to be what we're stuck with.
So if it's important to you to have this look, then you'll need to subclass QTabBar, override the tabSizeHint--I suspect that will take some experimentation--and use QTabWidget::setTabBar to replace the default with your own. According to the documentation, you have to do that before adding any tabs, so this mechanism is not workable if you want to create your tab widget in Qt Designer. (Another argument in favor of implementing setExpanding as an override to the OS style rather than the way it's been done.)
Just set both expanding and document mode to true.
ui->tabWidget->tabBar()->setDocumentMode(true);
ui->tabWidget->tabBar()->setExpanding(true);
Works for me :)
As mostefa answered here, I can set a fixed width for the tabs using styleSheet.
I am calculating the width based on the QTabWidget width.
To get the QTabWidget width correctly I need to get it in the showEvent function:
void LogListForm::showEvent(QShowEvent *ev)
{
/*
* Divide by 2 because we have 2 tabs.
* I need to decrease 24 pixels to fill the width correctly.
*/
int tabWidth = (ui->myTabWidget->width()/2)-24;
/*
* Then, I set this tabWidth to the styleSheet.
* Note: I need to set the previously styleSheet to not lose it
*/
ui->myTabWidget->setStyleSheet( ui->myTabWidget->styleSheet() +
"QTabBar::tab {"
"width: " + QString::number(tabWidth) + "px; }" );
}
This works as well:
QTabWidget::tab-bar
{
min-width:1000;
}
// Qt 5.12.2
// just use TabWidget in place of QTabWidget, nothing else
class TabWidget : public QTabWidget
{
class TabBar : public QTabBar
{
QSize _size;
public:
TabBar(QWidget* a_parent) : QTabBar(a_parent)
{
setWidth(size().width());
}
QSize tabSizeHint(int index) const
{
return QSize(_size.width()/(count()?count():1), size().height());
}
void setWidth(int a_width)
{
_size = QSize(a_width, size().height());
QTabBar::resize(_size);
}
};
TabBar* _tabBar = new TabBar(this);
public:
TabWidget(QWidget* a_parent) : QTabWidget(a_parent)
{
setTabBar(_tabBar);
}
void resizeEvent(QResizeEvent *e) override
{
_tabBar->setWidth(size().width());
QTabWidget::resizeEvent(e);
}
};
this worked for me:
ui->myTabWidget->tabBar()->setDocumentMode(true);
Suggestion from Mitak worked. Assuming the tabwiget is in the centralwidget of the mainwindow, one can replace a tabwidget created with the designer as follow in the code :
QTabWidget* newTabWidget = new GeneralTabWidget(ui->tabWidget_ToReplace->parentWidget());
ui->centralWidget->layout()->replaceWidget(ui->tabWidget_ToReplace, newTabWidget );
delete ui->tabWidget_ToReplace;
ui->tabWidget_ToReplace= newTabWidget ;
if the tabwidget is situated in another location or in a dialog, one need to replace it in the appropriate layout.
Not pretty, but what worked for me was to forced the value setExpending in the minimumSizeHint override.
class A: public QTabWidget
{
A(QWidget *p = nullptr): QTabWidget(p)
{
setDocumentMode(true);
}
virtual QSize minimumSizeHint() const override
{
tabBar()->setExpanding(true);
return QTabWidget::minimumSizeHint();
}
};

qt5 QGraphicsScene setBackgroundBrush() not always work when called frequently

I want to write an tool of objective annotation using qt5 MinGw32, which could annotate object in video file and diplay them during playing. So QGraphicsScene is inherited to implementation the function.
Something wrong happens when I change the QGraphicsScene's background frequently(e.g. 30 fps): most of time it works as expected while sometimes the background could not move.
Here is my code:
void MyGraphicsScene::UpdateFrame(QImage image)
{
QPixmap pixmap = QPixmap::fromImage(image);
//fix the view's size and scene's size
views().first()->setFixedSize(pixmap.size());
setSceneRect(0,0, pixmap.width(), pixmap.height());
setBackgroundBrush(QBrush(pixmap));
}
...
//In another thread
void Thread::run()
{
...
myScene.UpdateFrame(newImage);
...
}
I have search through the qt's document and found no answer.
However, there is something strange:
when wrong thing happens, I find the background continues to change, but it didn't show change on the screen unless I move the app to another screen (I have two screen). However, with the app moved, the QGraphicsScene's background just change once and becomes static afterwards.
I guess the background has been changed but doesn't repainted, so I used update(), but it didn't help.
BTW, I couldn't reproduce the occasion, sometiems it happens, somtimes not.
do I need to represented any methods? Or I called the methods in a wrong way? Or is there an alternative approach that would work?
Many thanks for your help in advance.
You should not change QtGui elements from a different thread by calling a method directly.
Use the Qt signal-slot concept.
Calling update() is not necessary.
class MyGraphicsScene{...
....
signals:
void singalFromThread(QImage image);
public:
//CTor
MyGraphicsScene()
{
connect(this, SIGNAL(singalFromThread(QImage)),this,SLOT(UpdateFrame(QImage)));
}
//Call this method from your thread
void updateForwarder(QImage image)
{
//Jump into gui thread
emit singalFromThread(image);
}
public slots:
void UpdateFrame(QImage image)
{
setBackgroundBrush(....);
}
};

How to recognize QMouseEvent inside child widgets?

EDIT and Some Self Critisicm
I tried both given solutions, which both solved my problem, and thus I thank you both! I marked the transparent solution as accepted because I thought it is was the easiest implementation when I only had one child widget, but I wish to share some insight for other beginners:
I first used QLabel, which apperently has enabled Qt::WA_TransparentForMouseEvents by default and thus obviously worked, but I also wanted the text to be selectable, by using QPlainTextEdit instead. Laughably, this is not possible in any way, because if you try to select the text (by clicking) you will close the window! I ended up keeping the transparancy, and neglecting the select-text-feature.
I'm guessing my following question has been answered somewhere before, but after an hour of searching I now post the question myself. I'm grateful if someone can point me to an already answered question that solves my problem.
Anyhow, I'm creating a popup window, using C++ and Qt. I've created the following PopupDialog class which works well and fine for all its purposes. However, I've removed its frame (including the bar containing the close button and window title) to make it look minimalistic, and now I want it to close if the user presses/releases the mouse button anywhere inside the popup window (dialog).
The below code works, however in such a way that I have to click and release the mouse exactly at the QDialog-window itself. It will not close when i click if the mouse hovers over the child widget(s) inside the QDialog, e.g. a QPlainTextEdit, which is displaying text.
Hence, I'm in need of a solution for QDialog to recognize QMouseEvents inside its child widgets. Please, don't hesitate to ask if something is unclear. I have not included my mainwindow.h/.cpp files, or popupdialog.ui file since I believe it would be a little too much to post here, but the .ui extremely simple: Just the QDialog window holding a QBoxLayout, containing a single widget, a QPlainTextEdit. I may posts these on request if it helps.
// popupdialog.h
#ifndef POPUPDIALOG_H
#define POPUPDIALOG_H
#include <QDialog>
#include <QString>
namespace Ui {class PopupDialog;}
class PopupDialog : public QDialog
{
Q_OBJECT
public:
explicit PopupDialog(QWidget *parent = 0, QString msgTxt="");
~PopupDialog();
private:
Ui::PopupDialog *ui;
QString messageText;
void mouseReleaseEvent(QMouseEvent*);
};
#endif //POPUPDIALOG_H
...
// popupdialog.cpp
#include "popupdialog.h"
#include "ui_popupdialog.h"
PopupDialog::PopupDialog(QWidget *parent, QString msgTxt) :
QDialog(parent),
ui(new Ui::PopupDialog),
messageText(msgTxt)
{
ui->setupUi(this);
setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
setModal(true);
ui->message_text_display->setText(messageText);
// The message_text_display is an instance of the class,
// "PlainTextEdit". Using "QLabel" partly solves my
// problem, but does not allow text selection.
}
PopupDialog::~PopupDialog()
{
delete ui;
}
void PopupDialog::mouseReleaseEvent(QMouseEvent *e)
{
this->close();
}
As you already noticed mouse events are handled from child widgets and propagated to parents if not accepted. You can read more about it here
To close your popup window when the click is done inside a child widget you can do two things. You could try looking into installEventFilter and set it up on each child widget to call close().
Another option would require you to have a kind of centralWidget (like the MainWindow usually has) - just to group all your child widgets. This way you could call setAttribute() on it to set Qt::WA_TransparentForMouseEvents property to simply skip handling mouse events on the widget and all of its children.
groupWidget->setAttribute(Qt::WA_TransparentForMouseEvents);
According to Qt docs:
When enabled, this attribute disables the delivery of mouse events to
the widget and its children. Mouse events are delivered to other
widgets as if the widget and its children were not present in the
widget hierarchy; mouse clicks and other events effectively "pass
through" them. This attribute is disabled by default.
Which basically means the event would be passed up the chain to the first widget which can handle the event. In your case it would be the PopupDialog and the already overriden mouseReleaseEvent slot.
in header file
class PopupDialog : public QDialog
{
Q_OBJECT
public:
explicit PopupDialog(QWidget *parent = 0, QString msgTxt="");
~PopupDialog();
//////////////////////////////////
protected:
bool eventFilter(QObject *obj, QEvent *event);
//////////////////////////////////////
private:
Ui::PopupDialog *ui;
QString messageText;
void mouseReleaseEvent(QMouseEvent*);
};
in cpp
PopupDialog::PopupDialog(QWidget *parent, QString msgTxt) :
QDialog(parent),
ui(new Ui::PopupDialog),
messageText(msgTxt)
{
ui->setupUi(this);
setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
setModal(true);
ui->message_text_display->setText(messageText);
// The message_text_display is an instance of the class,
// "PlainTextEdit". Using "QLabel" partly solves my
// problem, but does not allow text selection.
///////////////////////////////////////
foreach (QObject *child, children())
{
child->installEventFilter(this);
}
///////////////////////////////////////
}
///////////////////////////////////////
bool PopupDialog::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::MouseButtonRelease)
{
this->close();
}
}

Paint over top of label, not behind it in Qt

I am creating a simple gauge in Qt 4.7.4, and everything is working wonderfully. Except for the fact that, for the life of me, I cannot get the dial shape to paint over the text labels when it passes over them. It always paints it behind the label. I am just using a simple drawpolygon() method.
I'm thinking this has something to do about paint events? I am drawing everything inside a QFrame inside a MainWindow. I am using QFrame's paintEvent.
Edit:
The QLabels are created on start up with new QLabel(this). They are only created once, and never touched again ( Similar to manually adding them on the Ui with Designer). The drawpolygon() is in the QFrame's Paint event.
"myclass.h"
class gauge : public QFrame
{
Q_OBJECT
public:
explicit gauge(QWidget *parent = 0);
~gauge();
void setValues(int req, int Limit, bool extra=false);
private:
void drawDial();
protected:
void paintEvent(QPaintEvent *e);
};
"myclass.cpp"
void gauge::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
drawDial();
return;
}
void gauge::drawDial()
{
QPainter Needle(this);
Needle.save();
Needle.setRenderHint(Needle.Antialiasing, true); // Needle was Staggered looking, This will make it smooth
Needle.translate(centrePt); // Center of Widget
Needle.drawEllipse(QPoint(0,0),10,10);
Needle.restore();
Needle.end();
}
If the gauge widget and the QLabels are siblings, then you can move the gauge widget to the front by calling its raise() method.
If the QLabels are children of the gauge widget, on the other hand, then they will always display in front of it. In that case you can either reorganize your widget hierarchy so that they are siblings instead, or you can get rid of the QLabels and simply call drawText() from your paintEvent() method instead (after drawDial() returns)

Qt Update Pixmap of QGraphicsPixmapItem

I am using the QGraphicsPixmapItem to show an image on the display. Now, I want to be able to update this image on the fly, but I seem to be running into some issues.
This is the header file:
class Enemy_View : public QGraphicsPixmapItem
{
public:
Enemy_View(QGraphicsScene &myScene);
void defeat();
private:
QGraphicsScene &scene;
QPixmap image;
}
And here is the cpp file
Enemy_View::Enemy_View(QGraphicsScene &myScene):
image{":/images/alive.png"}, scene(myScene)
{
QGraphicsPixmapItem *enemyImage = scene.addPixmap(image.scaledToWidth(20));
enemyImage->setPos(20, 20);
this->defeat();
}
void Enemy_View::defeat(void)
{
image.load(":/images/dead.png");
this->setPixmap(image);
this->update();
}
SO the idea is that I want to be able to call the defeat method on my object, that then edits some attributes and eventually changes the image. However, what I am doing now does not work. The alive.png image does display, but does not get updated to the dead.png one.
Update 1
As mentioned by Marek R I appear to be replicating a lot of built-in functionality. I tried to clean this up but now nothing is showing up on the scene anymore.
.h file
class Enemy_View : public QGraphicsPixmapItem
{
public:
Enemy_View(QGraphicsScene &myScene);
void defeat();
private:
QGraphicsScene &scene;
/* Extra vars */
};
.cpp file
Enemy_View::Enemy_View(QGraphicsScene &myScene):
scene(myScene)
{
/* This part would seem ideal but doesn't work */
this->setPixmap(QPixmap(":/images/alive.png").scaledToWidth(10));
this->setPos(10, 10);
scene.addItem(this);
/* This part does render the images */
auto *thisEl = scene.addPixmap(QPixmap(":/images/Jackskellington.png").scaledToWidth(10));
thisEl->setPos(10, 10);
scene.addItem(this);
this->defeat();
}
void Enemy_View::defeat(void)
{
this->setPixmap(QPixmap(":/images/dead.png"));
}
So I removed the QPixmap, but I'm not sure whether I can remove the QGraphicsScene. In my cpp-file you can see I have two versions of the constructor now. The first part, using the this seems like an ideal solution, but does not display the image on the screen (even though it does compile without errors). The second version with thisEl does render it. What am I doing wrong with the first version?
Why FGS you are subclassing QGraphicsPixmapItem? QGraphicsPixmapItem has all functionality you need. And those new fields you have added does nothing, they only try replicate functionality which is already there (but with this implementation it does nothing).
This suppose to be something like that:
QPixmp image(":/images/alive.png");
QGraphicsPixmapItem *enemyItem = scene.addPixmap(image.scaledToWidth(20));
enemyItem->setPos(20, 20);
// and after something dies
QPixmap dieImage(":/images/dead.png");
enemyItem->setPixmap(dieImage);