Setting up the position of QGraphicsView

  • Hello everyone. I am a beginner in Qt. Am developing a GUI on linux platform and am using two QGraphicsView in my GUI. I want to know how to set the position of QGraphicsView. As both the graphics view is being overlapped. I would really appreciate if anyone could help me by providing the information on setting the position of QGraphicsView.

    Thanks in advance,

  • Well, you either use "absolute positioning": or you use "layouts":

  • Hi,thanks for your reply.... is their an example code that I code refer to.... as I told you am a beginner it would be really helpful,,

  • the code is as follows:

    graphicsView= new QGraphicsView;

    [EDIT: code formatting, Volker]

  • I tried using absolute positioning but its the same result. I also tried using layouts but am getting the comment as program has unexpectedly finished. Please see the code,

    horizontalLayout = new QHBoxLayout(widget);
    horizontalLayout->setContentsMargins(0, 0, 0, 0);

    graphicsView = new QGraphicsView;//(this);
    graphicsView->setStatusTip(tr("Area to Display the Image"));
    graphicsView->setGeometry(QRect(10, 40, 256, 192));
    graphicsView->setStyleSheet("color: black;"
                              "background-color: gray;"
                              "selection-color: yellow;"
                              "border: 3px solid black;"
                              "border-radius: 40px;");

    graphicsView1 = new QGraphicsView;//(this);
    graphicsView1->setStatusTip(tr("Area to Display the Image"));
    graphicsView1->setGeometry(QRect(300, 40, 256, 192));
    graphicsView1->setStyleSheet("color: black;"
    "background-color: gray;"
    "selection-color: yellow;"
    "border: 3px solid black;"
    "border-radius: 40px;");

     scrollArea = new QScrollArea;


    [EDIT: code formatting, Volker]

  • At first please wrap your code around @-tags and format it properly. It is an unreadable mess.

    As to your code:

    • You are missing a call to the base class constructor (QMainWindow).
    • horizontalWidget has widget as its parent, which is neither declared nor initialized.
    • QScrollArea can hold just one widget (set... is an indicator that there can be set just one object, add... would be an indicator that there can be set multiple objects).
    • By passing graphicsView1 to QScrollArea::setWidget() graphicsView is deleted as it was implicitly reparented to the QScrollArea when set.

    Just create horizontalLayout without a parent, set it as layout to an ordinary QWidget instead of a QScrollArea (QGraphicsView already implements QAbstractScrollArea) and set this widget as the central widget.

    If there is anything you haven't understood - especially parenting or the layout system - you should have a good read on Qt fundamentals first. If there are any specific questions feel free to come back here.

  • Thanks a lot for your reply.... Yes, now I have modified my code and its working fine, now I can display two GraphicsView windows in the GUI. I have to work out on layouts,, I will definitely comeback if I have any queries. Thanks a ton :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.