1
votes

I would like to make a QGraphicsView just fit in a QMainWiondw of fixed size. And I need the coordinate of the top left corner to be (0,0). How can I do this?

This is my main function

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow window;
    window.setFixedSize(1440, 900);

    window.show();

    return a.exec();
}

This is the MainWindow constructor.

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    view = new QGraphicsView(this);
    scene = new QGraphicsScene(0, 0, 1440, 900, this);
    view->setScene(scene);
    setCentralWidget(view);

    drawCoordinate(0,0);
}

There is a scroll appeared. I think it is deal to the title bar.

I don't really mind the size of the QGraphicsView as long as the size of the mainwindow is fixed. But I need the QGraphicsView to fill the whole mainwindow without a scroll bar and the top left corner of the mainwindow is located at (0, 0).

With @tangerine help, I can make the program like this. But there is still space between the main window and the graphic view. Program Output

This is what I hope to achieve: enter image description here

The (0, 0) coordinate starts at the topmost and leftmost of the mainwindow.

2
I had the same problem. Solved with view->setAlignment(Qt::AlignLeft | Qt::AlignTop);metalfox

2 Answers

1
votes

You need to use a layout for the main window and put the graphics view into it. You can find more about this topic here: How to make a Qt Widget grow with the window size?

0
votes

If I've understood you correctly I think you should add this string in the MainWindow constructor: view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);