Widget container or layout with free positioning of widgets and with scroll bars

  • hi,
    i already tried a lot but without luck and i searched for similar problems and solutions...

    i've got a window with a vertical layout which has two horizontal layouts with some widgets.
    what i want to build is a part (see it at the bottom of the picture) with some widgets where i can set the size of and the position them.

    it shall be some kind of timeline but it's not only for painting these boxes, the user should be able to click them and the program should react....so i guess it's better if these are widgets rather than it's "only" a painting.

    do you have any hint for me how to solve this problem?


  • Maybe use a "QScrollArea":http://qt-project.org/doc/qt-5.1/qtwidgets/qscrollarea.html with a "QGridLayout":http://qt-project.org/doc/qt-5.1/qtwidgets/qgridlayout.html inside? Alternatively, you can position widgets completely without a layout - assign a parent, call their QWidget::move() (coordinates relative to parent) function and QWidget::show() them. But that's very unflexible.

    Or do you want to be able to drag the widgets around via mouse? That would be possible, too. First step should be a QScrollArea nonetheless. Next step would be to reimplement some functions (mousePressEvent, dropEvent, ...). I would suggest one of "these examples":http://qt-project.org/doc/qt-5.1/qtdoc/examples-draganddrop.html for that.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You might also be interested by "the graphics view framework":http://qt-project.org/doc/qt-4.8/qgraphicsview.html

  • Thanks for the replies. I got a solution now.
    I already tried to use a QScrollArea but my problem was that I could only add one widget and i didn't have any luck with added layouts to to work with the scrollbars.
    That is what i tried:
    @ QGridLayout* lay = new QGridLayout(ui->scrollArea1);
    QImage* img = new QImage(800,800, QImage::Format_ARGB32);

    QLabel* myLabel = new QLabel();

    In my new solution I will use a label as background set as widget in the scrollarea and i will set the parent of the other time-bar-labels to the background labels:

    @ QLabel* myLabel = new QLabel();
    myLabel->setStyleSheet("QLabel { background-color : white; color : black; }");

    QLabel* myLabel2 = new QLabel("Label");
    myLabel2->setStyleSheet("QLabel { background-color : red; color : black; }");

    I will also look at the QGraphicsView class.

Log in to reply

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