Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWidget with ScrollArea as standalone window
Qt 6.11 is out! See what's new in the release blog

QWidget with ScrollArea as standalone window

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 838 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • LeoCL Offline
    LeoCL Offline
    LeoC
    wrote on last edited by
    #1

    Hey,

    what I need is a dynamically created qWidget with a Scrollarea and a reasonable size.

    First I tried creating a class like the following:

    class myWindow: public QWidget
    

    Inside this class I created a QScrollArea and set the widget (the class itself) to the ScrollArea.
    To show this window I executed show on the scrollArea.

    scrollArea->setWidget(this);
    scrollArea->show();
    

    Actually this worked pretty good. The layout was shown as expected and was wrapped inside a scollArea.
    The issues started when I wanted to implement a closeEvent. By pressing the X of the window, it actually never called the closeEvent. I assume it didn't happend, because I actually didn't close the widget itself but rather the scrollArea. So actually the closeEvent of the scrollArea was executed.

    So I changed the definition of my class to

    class myWindow: public QScrollArea
    

    Hitting the "X" of the window, it now executes the closeEvent and I can save some values before the window actually closes.

    But now the layout is completey messed up. I have a QGridLayout which is set to "this", but now the QScrollArea tries to display the entire Layout, but it is much more than you can put on a normal screen. So, the window is huge. That is what the attached code produces.

    When I try to resize (like this->resize(500,500) ) the scrollarea, my layout is completey squeezed. It looks like the whole inner layout is pushed inside these 500x500. But in this case I do not need the scrollbar anymore, because everything is visible now :)
    Right now, my implementation of myWindow looks like the following. What I need is the layout with is original size and the scrollArea around it. Any ideas?

    #include "myWindow.h"
    
    myWindow::myWindow(QWidget *parent) : QScrollArea(parent)
    {
    
    }
    
    void myWindow::addValues(QList<QStringList> strL_IN) {
       
        //create new grid layout
        QGridLayout *gL_AED = new QGridLayout();
    
        for(int i_CtrLengthOfList = 0; i_CtrLengthOfList < strL_IN.count(); i_CtrLengthOfList++) {
    
            //create a qLabel
            gL_AED->addWidget(new QLabel("Label1",i_CtrLengthOfList,0);
    
            //create a qTextEdit 
            QTextEdit *tE_ValuesOneRow = new QTextEdit();
            l_TextEdit.append(tE_ValuesOneRow);
    
            //set the html formatted text to the qTextEdit
            tE_ValuesOneRow->setHtml(strL_IN.at(i_CtrLengthOfList).at(1));
    
            QFont f_serifFont("Times", 10, QFont::Bold);
            QFontMetrics fM_serifFont(f_serifFont);
    
            //calculates the width and height of the qTextEdit
            int i_Width= fM_serifFont.horizontalAdvance(tE_ValuesOneRow->toPlainText());
            int i_Height= fM_serifFont.height();
    
            //set the width and height of the qTextEdit
            tE_ValuesOneRow->setFixedSize(i_Width > 50 ? i_Width : 50,i_Height + 12);
    
            //add the qTextEdit to gridlayout
            gL_AED->addWidget(tE_ValuesOneRow,i_CtrLengthOfList,1);
        }
    
        //create the save button. Add the button to the layout
        QPushButton *pB_Save = new QPushButton("Save");
        gL_AED->addWidget(pB_Save,strL_IN.count()+1, 0);
        connect(pB_Save,SIGNAL(clicked()),this,SLOT(saveValues()));
    
        this->setLayout(gL_AED);
    }
    
    void myWindow::closeEvent(QCloseEvent *event) {
        qDebug() << "x was pressed";
        event->accept();
    }
    
    void myWindow::showMe() {
        this->show();
    }
    
    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      You shouldn't set a layout to a scroll area (aka. call this->setLayout(gL_AED)).
      Instead, you should create a QWidget, then

      widget->setLayout(gL_AED);
      this->setWidget(widget);
      
      1 Reply Last reply
      4
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Beside @Bonnie good points, there's rarely a need to subclass QScrollArea. It's usually a class you use to put a widget in it rather that make the widget parts of it.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • LeoCL Offline
          LeoCL Offline
          LeoC
          wrote on last edited by
          #4

          well... after the weekend I just read my post again and suddenly I had the following idea in my mind:

          my class is sublcassing QWidget instead of QScrollArea now

              w_WrapWidget->setLayout(gL_AED);
              QScrollArea *sA_ScrollArea = new QScrollArea();
              sA_ScrollArea->setWidget(w_WrapWidget);
              QHBoxLayout *hb_Layout = new QHBoxLayout();
              hb_Layout->addWidget(sA_ScrollArea);
              this->setLayout(hb_Layout);
          

          What sall I say to this? The result is exactly what I wanted to have :)

          Sometimes it really makes sense to take a break while programming in order to get fresh ideas.

          Thanks for your hints @Bonnie @SGaist

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved