maxwellhertz
maxwellhertz

Reputation: 483

How to keep the graphics in the center of main window

I am learning Qt5 and I use Qt 5.9.1. Now I have a problem: how to keep the graphics in the center of main window even when the size of main window changes?

Last year I learned MFC in my class and the teacher told us that in order to make the graphics always stay in the window, we should do as followings:

  1. Let the origin of viewport be the center of client area;
  2. Let the size of viewport be the size of client area;
  3. Let the origin of window be the center of graphics' bounding rectangle;
  4. Let the size of window be the size of graphics' bounding rectangle .

So I do the same thing in Qt5:

// main.cpp
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QtGuiApplication1 w;
    // get the size and center of client area
    // and then pass the values to QtGuiApplication
    int width = QApplication::desktop()->availableGeometry().width();
    int height = QApplication::desktop()->availableGeometry().height();
    QPoint center = QApplication::desktop()->availableGeometry().center();
    w.setViewport(center,width,height);

    w.show();
    return a.exec();
}

// GtGuiApplication.cpp 
void QtGuiApplication1::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    // set the viewport
    painter.setViewport(centerOfViewport.x(),centerOfViewport.y(), widthOfViewport, heightOfViewport);

    static const QPointF points[4] = {
    QPointF(10.0, 10.0),
    QPointF(10.0, 80.0),
    QPointF(50.0, 80.0),
    QPointF(10.0, 10.0)
    };

    // set the window
    painter.setWindow(30,45,40,70);

    painter.drawPolyline(points, 4);
}

However all these things didn't work. Before I set the viewport and window:

enter image description here

And after I did the setting:

enter image description here

Upvotes: 1

Views: 836

Answers (1)

eyllanesc
eyllanesc

Reputation: 244301

I do not understand what you indicate in the line, maybe it is fulfilled within MFC, but the rules are not accepted without analyzing them, you have to understand them and it seems that you want to apply them without understanding them correctly.

According to what you point out, you want the polygon to always be centered within the window, and in your code you use the size of the window which does not make sense since the window can be anywhere on the screen, from there we go badly.

If you want the center of the polygon to be the center of the window, then you must calculate both points considering in the first reference source the topleff of the rectangle bordering the polygon, and in the second the rectangle of the window, if we subtract both positions we obtain what must be moved by the painter so that both points coincide.

void QtGuiApplication1::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    static const QVector<QPointF> points = {
        QPointF(10.0, 10.0),
        QPointF(10.0, 80.0),
        QPointF(50.0, 80.0),
        QPointF(10.0, 10.0)
    };
    QPainterPath path;
    path.addPolygon(QPolygonF(points));
    QPointF center_path = path.boundingRect().center();
    painter.translate(rect().center()-center_path);
    painter.drawPath(path);
}

enter image description here

enter image description here

Upvotes: 3

Related Questions