Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to auto-resize qt widget?
    text
    copied!<p>QT Version: 4.5.2 OS: ubuntu 8.04</p> <p>I have a trouble with auto-resizing widget, here is an example:</p> <p><a href="http://img15.imageshack.us/img15/3479/autoresize.png" rel="nofollow noreferrer">alt text http://img15.imageshack.us/img15/3479/autoresize.png</a></p> <p>As the figure shows, here are two widgets, one is a "StrWidget"(the upper widget in the figure 1) which has 5 QLabels for showing strings, the other is a "CtrlWidget" which tells StrWidget what string to show.</p> <p>When I click "send CCC", the StrWidget becomes the widget shown in figure 2</p> <p>But when I click "send A" again, the StrWidget becomes the widget shown in figure 3</p> <p>There is a member function StrWidget::changeString which is a slot of StrWidget and receive the string for showing.</p> <pre><code>void StrWidget::changeString(QString inputStr) { for(int i=0;i&lt;5;i++){ strEntries[i]-&gt;setText(inputStr); } } </code></pre> <p>the StrWidget cannot auto-resize itself when changeString is called. I try the following two methods:</p> <p>(1) Insert "adjustSize()" in StrWidget::changeString, it works but I need to click a button twice to make it resized. I don't know why it happens.</p> <p>(2) Insert "hide();show();" in StrWidget::changeString, it works but the StrWidget would flush when I click buttons.</p> <p>Dose anyone have an idea about it? Thanks~</p> <hr> <p>The following is the source code of the example</p> <p>main:</p> <pre><code>#include "StrWidget.h" #include "CtrlWidget.h" #include &lt;QApplication&gt; int main(int argc,char *argv[]) { QApplication app(argc,argv); StrWidget *strWidget=new StrWidget(0); CtrlWidget *ctrlWidget=new CtrlWidget(0); strWidget-&gt;show(); ctrlWidget-&gt;show(); QObject::connect(ctrlWidget,SIGNAL(sendString(QString)),strWidget,SLOT(changeString(QString))); int ref=app.exec(); strWidget-&gt;deleteLater(); ctrlWidget-&gt;deleteLater(); return ref; } </code></pre> <p>StrWidget.h</p> <pre><code>#include &lt;QWidget&gt; #include &lt;QString&gt; #include &lt;QLabel&gt; #ifndef _StrWidget_H_ #define _StrWidget_H_ class StrWidget:public QWidget { Q_OBJECT public: StrWidget(QWidget *parent=0); private: QLabel *strEntries[5]; public slots: void changeString(QString inputStr); void clearString(); }; #endif </code></pre> <p>StrWidget.cpp</p> <pre><code>#include "StrWidget.h" #include &lt;QPushButton&gt; #include &lt;QHBoxLayout&gt; StrWidget::StrWidget(QWidget *parent):QWidget(parent) { //main layout QHBoxLayout *mainLayout=new QHBoxLayout(this); setLayout(mainLayout); //info label setup for(int i=0;i&lt;5;i++){ strEntries[i]=new QLabel(this); strEntries[i]-&gt;setText("A"); strEntries[i]-&gt;setFrameShape(QFrame::StyledPanel); mainLayout-&gt;addWidget(strEntries[i]); } //clearBtn setup QPushButton *clearBtn=new QPushButton(tr("Clear Str"),this); connect(clearBtn,SIGNAL(clicked()),this,SLOT(clearString())); mainLayout-&gt;addWidget(clearBtn); } void StrWidget::changeString(QString inputStr) { for(int i=0;i&lt;5;i++){ strEntries[i]-&gt;setText(inputStr); } } void StrWidget::clearString() { changeString(""); } </code></pre> <p>CtrlWidget.h</p> <pre><code>#include &lt;QWidget&gt; #include &lt;QString&gt; #ifndef _CtrlWidget_H_ #define _CtrlWidget_H_ class CtrlWidget:public QWidget { Q_OBJECT public: CtrlWidget(QWidget *parent=0); signals: void sendString(QString sentStr); }; </code></pre> <p>CtrlWidget.cpp</p> <pre><code>#include "CtrlWidget.h" #include &lt;QHBoxLayout&gt; #include &lt;QPushButton&gt; #include &lt;QSignalMapper&gt; CtrlWidget::CtrlWidget(QWidget *parent):QWidget(parent) { //main layout QHBoxLayout *mainLayout=new QHBoxLayout(this); setLayout(mainLayout); //control btns setup QPushButton *sendABtn=new QPushButton("send A",this); QPushButton *sendBBBtn=new QPushButton("send BB",this); QPushButton *sendCCCBtn=new QPushButton("send CCC",this); mainLayout-&gt;addWidget(sendABtn); mainLayout-&gt;addWidget(sendBBBtn); mainLayout-&gt;addWidget(sendCCCBtn); //map setup QSignalMapper *btnMapper=new QSignalMapper(this); btnMapper-&gt;setMapping(sendABtn,"A"); btnMapper-&gt;setMapping(sendBBBtn,"BB"); btnMapper-&gt;setMapping(sendCCCBtn,"CCC"); connect(btnMapper,SIGNAL(mapped(QString)),this,SIGNAL(sendString(QString))); //connection setup connect(sendABtn,SIGNAL(clicked()),btnMapper,SLOT(map())); connect(sendBBBtn,SIGNAL(clicked()),btnMapper,SLOT(map())); connect(sendCCCBtn,SIGNAL(clicked()),btnMapper,SLOT(map())); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload