QT zoom to Polygon - c++

i have a fixed size QGraphicsview and my own class QGraphWidget class
GraphWidget::GraphWidget(QWidget *parent)
:QGraphicsView(parent)
{
QGraphicsScene *scene = new QGraphicsScene(this);
setScene(scene);
scale(1,-1);
setWindowTitle(tr("Poly"));
}
void GraphWidget::showPoly(){
scene()->clear();
scene()->setSceneRect(QRectF());
QPolygonF polygon;
QPen pen(Qt::black,1);
QBrush brush(Qt::black);
brush.setStyle(Qt::SolidPattern);
polygon<< QPointF(0,0)<< QPointF(10,0)<< QPointF(15,20)<<QPointF(5,10);
;
QGraphicsPolygonItem *polyItem=scene()->addPolygon(polygon,pen);
fitInView(polyItem);
}
It is possible that the scene focus the polygon and zoom it ?
I tried fitinView() or setSceneRect() but nothing worked, the polygon is still very small.
EDIT
with fitInView(polyItem->boundingRect());
The problem is that my QGraphicsview is fixed, so the fit in view has to zoom in. It can't change the size of the Qgraphicsview

So i got the answer.
The problem was that i called showPoly() inside of the constructor. But on this moment the size of the Qgraphicsview was not fixed.
I use a work around with QSingleShot
#include "mainwindow.h"
#include <QApplication>
#include <QTimer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QTimer::singleShot(1, &w,SLOT(callShowPoly()));
return a.exec();
}

Related

How to launch the QMainWindow with a fading animation?

I tried launching my window this way:
#include "stdafx.h"
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setWindowOpacity(0);
QGraphicsOpacityEffect* eff = new QGraphicsOpacityEffect(this);
QPropertyAnimation* ani = new QPropertyAnimation(eff, "windowOpacity");
ani->setDuration(3000);
ani->setStartValue(0);
ani->setEndValue(1);
ani->setEasingCurve(QEasingCurve::OutBack);
ani->start(QPropertyAnimation::DeleteWhenStopped);
}
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
But the window never became visible, I would like to it be initialized with a fade in effect.
What's the proper way to achieve it?
Two issues I see. First, as originally written, your main function is going to exit immediately after opening the window. Add return a.exec(); at the end of main.
Next, you are animating QGraphicsOpacityEffect. As written, your example code has no connection between QGraphicsOpacityEffect and the window. Your animation is animating the property on the effect class, but that doesn't propagate back to the widget. There is no need to use QGraphicsOpacityEffect. Instead, just animate the widget directly:
ui.setupUi(this);
setWindowOpacity(0);
// Notice that the first argument passed in is 'this' - the widget.
// the widget is what you want to animate.
QPropertyAnimation* ani = new QPropertyAnimation(this, "windowOpacity");
ani->setDuration(3000);
ani->setStartValue(0);
ani->setEndValue(1);
ani->setEasingCurve(QEasingCurve::OutBack);
ani->start(QPropertyAnimation::DeleteWhenStopped);

Fill background outside window QT

I have read a documentation and aswell many sites on website.
Unfortunatly I didn't find answer to my question.
Is there any chance(I believe there is) to fill background outside the popup window? Let me explain: If i have window like of my whole app with resolution 500x500 [px] and I create a popup window 300x300 in the middle - it means I have 200 px in each side "parent-window". Is there any chance (method, flag) to fill background in gray color?
Image: https://imgur.com/Hunev58
Modifying the palette does the job. Here you get a purple background when the MessageBox is shown , and come back to normal once clicked
#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QMessageBox>
#include <QPalette>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
QVBoxLayout mainLayout;
QLabel foolabel("FooLabel");
QPushButton foobutton("FooButton");
mainLayout.addWidget(&foolabel);
mainLayout.addWidget(&foobutton);
QWidget window;
window.setLayout(&mainLayout);
w.setCentralWidget(&window);
QPalette palette = QApplication::palette(&w);
palette.setColor(QPalette::Inactive,QPalette::Window,QColor(138,43,226));
w.setPalette(palette);
w.show();
QMessageBox box(&w);
box.exec();
return a.exec();
}

Qt: cursor blinking cause repaint of parent widget?

Bellow you can see minimal example code to demonstrate problem.
If you run it and give focus to QLineEdit, you get output every second: paintEvent, paintEvent and so on.
I can not understand why MyW::paintEvent called on every
cursor blinking in child widget? As you see I do not configure QLineEdit,
by default on my linux box it have no transparent elements,
but still for some reason cursor cause all widgets to redraw their content?
#include <QApplication>
#include <QWidget>
#include <QPaintEvent>
#include <QPainter>
#include <QLineEdit>
class MyW final : public QWidget {
public:
MyW() {
//setAutoFillBackground(false);
}
void paintEvent(QPaintEvent *e) {
e->accept();
qDebug("paintEvent");
QPainter painter{this};
painter.fillRect(rect(), Qt::green);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyW w;
w.resize(600, 600);
w.show();
auto le = new QLineEdit{&w};
le->show();
return app.exec();
}

move QGraphicsView around QGraphicsScene

I'm making a side view drag racing game in QT c++. I want to move my view around my scene from left to right. I have the scene set to 3600x800, but i want the view the be at the far left of my scene not at the center at the start. When i press W on my keyboard I want the view to move to the left for 1px. How do I do that? I can't find anything online
scene=new QGraphicsScene(this);
view = new QGraphicsView;
scene->setSceneRect(0,0,3600,800);
view->setScene(scene);
You will never find something so particular on the internet, you should look for each part separately:
If you want it to appear on the left side then you must use horizontalScrollBar() of the GraphicsView when it is displayed, we can do that with the showEvent method.
if you want to do an action when you press any key you could overwrite the keyPressEvent method.
To move the sceneRect() you must make a copy, move it and set it again.
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QKeyEvent>
#include <QScrollBar>
class GraphicsView: public QGraphicsView{
public:
using QGraphicsView::QGraphicsView;
protected:
void keyPressEvent(QKeyEvent *event){
if(event->key() == Qt::Key_W){
if(scene()){
QRectF rect = scene()->sceneRect();
rect.translate(1, 0);
scene()->setSceneRect(rect);
}
}
}
void showEvent(QShowEvent *event){
QGraphicsView::showEvent(event);
if(isVisible()){
horizontalScrollBar()->setValue(0);
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
GraphicsView view;
scene.setSceneRect(0,0,3600,800);
view.setScene(&scene);
scene.addRect(0, 200, 400, 400, Qt::NoPen, Qt::red);
view.show();
return a.exec();
}

Does QGraphicsTextItem support vertical-center alignment?

Basically, I want to know why the combination of text alignment flags and setPageSize doesn't end up with text centered in the display.
The following program does nearly exactly what I want, except that the text ends up centered only horizontally.
#include <QApplication>
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *mainWindow = new QMainWindow(0, Qt::FramelessWindowHint);
QGraphicsView view;
view.setAlignment(Qt::AlignLeft | Qt::AlignBottom);
view.setFrameStyle(0);
view.setBackgroundBrush(QBrush(QColor(Qt::black)));
mainWindow->setCentralWidget(&view);
QGraphicsScene scene(0, 0, 640, 480);
QGraphicsTextItem textItem;
textItem.setTextWidth(640);
textItem.document()->setPageSize(QSizeF(640, 480));
textItem.document()->setDocumentMargin(0);
textItem.document()->setDefaultTextOption(QTextOption(Qt::AlignCenter | Qt::AlignVCenter));
textItem.setDefaultTextColor(QColor(Qt::white));
textItem.setFont(QFont("monospace", 18, 63));
textItem.setHtml("Center me!");
scene.addItem(&textItem);
textItem.setVisible(true);
view.setScene(&scene);
mainWindow->show();
return a.exec();
}
I should also note that this project is constrained to Qt 4.7.1.
How do I align text both horizontally and vertically in the center of a QGraphicsView using a QGraphicsTextItem? I'm fine with a stylesheet-based solution as well.
No, it doesn't.
This bug indicates that the QTextDocument (the underlying text rendering object) specifically does not attempt to vertical alignment. After looking about, most workarounds fall into two categories. For simple (i.e. single-line plain-text) applications, implement a proxy item that renders the text in its overloaded paint method as so:
class MyProxyGraphicsItem: public QGraphicsItem {
public:
explicit MyProxyGraphicsItem(QString text, QRectF geometry, QGraphicsItem *parent=0) :
QGraphicsItem(parent), text(text), geometry(geometry)
{}
virtual ~MyProxyGraphicsItem() {}
QRectF boundingRect() const { return geometry; }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawText(geometry, text, Qt::AlignCenter | Qt::AlignVCenter);
}
private:
QRectF geometry;
QString text;
}
For multi-line plain-text or rich-text (HTML), use a QLabel in stead as follows:
#include <QApplication>
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *mainWindow = new QMainWindow(0, Qt::FramelessWindowHint);
QGraphicsView view;
view.setAlignment(Qt::AlignLeft | Qt::AlignBottom);
view.setFrameStyle(0);
view.setBackgroundBrush(QBrush(QColor(Qt::black)));
mainWindow->setCentralWidget(&view);
QGraphicsScene scene(0, 0, 640, 480);
QLabel label("<div style=\"color:white;\">Center me!</div>");
label.setWordWrap(true);
label.setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
label.setFont(QFont("monospace", 18, 63));
scene.addWidget(&label)->setGeometry(QRectF(0,0,480,640));
view.setScene(&scene);
mainWindow->show();
return a.exec();
}