QWidget with ScrollArea as standalone window
-
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(); }
-
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.