2

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.

tom
  • 1,302
  • 1
  • 16
  • 30

2 Answers2

1

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?

Community
  • 1
  • 1
ni1ight
  • 327
  • 4
  • 12
0

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

lubsandodok
  • 1
  • 1
  • 1
  • 1
    Thanks for your reply. I appreciate your help. It makes the scene fit into the view. But the only problem is the view is not fit into the mainwindow. – tom Oct 26 '15 at 06:59