Qt-interest Archive, October 2006
Now I can ask my REAL question! -- resizing a window
Message 1 in thread
So, I now have a simple reproducer for a basic problem I have with Qt.
The below code displays a simple dialog that has a button on it that
shows another button if clicked. When the new button is clicked, it
hides that button.
I would like to tell the dialog to get smaller after hiding the
second button, exactly as if the user had dragged the dialog to make
it as small as the first button would let it. So I put a resize()
command in on_disappear_clicked(). Unfortunately, this has no
effect. The dialog size remains unchanged from when it had two
buttons. If I manually resize it with my mouse, then click "show"
again, it resizes to fit the second button, which is nice, but I want
it to shrink again when the second button disappears.
I want the dialog to remain resizable by the user, by the way.
Can anyone tell me why this doesn't work?
Thanks! This has been bugging me for weeks.
#include "QMainWindow"
#include "QPushButton"
#include "QHBoxLayout"
#include "QVBoxLayout"
class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent=NULL): QMainWindow(parent) {
setCentralWidget(new QWidget(this));
QVBoxLayout *layout = new QVBoxLayout;
changer = new QPushButton("show");
layout->addWidget(changer);
centralWidget()->setLayout(layout);
disappear = new QPushButton("disappear");
layout->addWidget(disappear);
disappear->hide();
connect(changer, SIGNAL(clicked()), this, SLOT(on_changer_clicked
()));
connect(disappear, SIGNAL(clicked()), this, SLOT
(on_disappear_clicked()));
return;
}
private slots:
void on_changer_clicked(){
printf("showing...\n");
disappear->show();
return;
}
void on_disappear_clicked() {
printf("hiding...\n");
disappear->hide();
resize(1,1);
}
public:
QPushButton *changer;
QPushButton *disappear;
};
#include "test.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow blah(NULL);
blah.show();
return app.exec();
}
--
[ signature omitted ]
Message 2 in thread
On 02.10.06 19:30:55, Rich Cook wrote:
> I would like to tell the dialog to get smaller after hiding the second button,
> exactly as if the user had dragged the dialog to make it as small as the first
> button would let it. So I put a resize() command in on_disappear_clicked().
> Unfortunately, this has no effect. The dialog size remains unchanged from when
> it had two buttons. If I manually resize it with my mouse, then click "show"
> again, it resizes to fit the second button, which is nice, but I want it to
> shrink again when the second button disappears.
> I want the dialog to remain resizable by the user, by the way.
I was about to refer you to the extension example, but that one uses a
fixed-size layout. Small experiment shows that you're not going to get
what you want without some effort.
How about this:
When button 1 is clicked save the current size in a member variable,
then later on when the 2nd is clicked resize to that saved dialog size.
Some comments on the code, feel free to ignore ;)
> #include "QMainWindow"
> #include "QPushButton"
> #include "QHBoxLayout"
> #include "QVBoxLayout"
This doesn't work on all compilers I think, better use <QMainWindow>.
> MainWindow(QWidget *parent=NULL): QMainWindow(parent) {
In Non-MS C++ land 0 is commonly used instead of NULL, however I don't
know the pros/cons of either usage.
> private slots:
> void on_changer_clicked(){
> printf("showing...\n");
Do #include <QDebug> and then use C++ style printf-debugging: qDebug() << "showing";
> resize(1,1);
I guess this resize is ignored due to the too small size.
Andreas
--
[ signature omitted ]
Message 3 in thread
Rich,
Try this: adjust the window size +/- the size of your disappearing widget
and the layout's spacing between widgets. Not perfect, but it's along the
lines you wanted.
This would probably work best if the stuff you're hiding was embedded in a
QWidget or QFrame, or was a single custom widget.
#include <QMainWindow>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent=0): QMainWindow(parent) {
setCentralWidget(new QWidget(this));
layout = new QVBoxLayout;
changer = new QPushButton("show");
changer->setSizePolicy(QSizePolicy::Preferred,
QSizePolicy::Preferred); // big buttons
layout->addWidget(changer);
disappear = new QPushButton("disappear");
disappear->setSizePolicy(QSizePolicy::Preferred,
QSizePolicy::Preferred); // big buttons
layout->addWidget(disappear);
centralWidget()->setLayout(layout);
disappear->hide();
connect(changer, SIGNAL(clicked()), this,
SLOT(on_changer_clicked()));
connect(disappear, SIGNAL(clicked()), this,
SLOT(on_disappear_clicked()));
return;
}
private slots:
void on_changer_clicked(){
if (!disappear->isVisible()){
QSize addSize = disappear->size();
resize(width(), height() + addSize.height() +
layout->spacing());
disappear->show();
}
}
void on_disappear_clicked() {
QSize eliminateSize = disappear->size();
disappear->hide();
resize(width(), height() - eliminateSize.height() -
layout->spacing());
}
protected:
QPushButton *changer;
QPushButton *disappear;
QVBoxLayout *layout;
};
There's an initial resize that I can't get rid of. Mucking about with the
stretch factors an size policies didn't seem to produce exactly what you'd
expect.
Glen.
On 10/2/06, Rich Cook <wealthychef@xxxxxxxxx> wrote:
>
> So, I now have a simple reproducer for a basic problem I have with Qt.
>
> The below code displays a simple dialog that has a button on it that shows
> another button if clicked. When the new button is clicked, it hides that
> button.
> I would like to tell the dialog to get smaller after hiding the second
> button, exactly as if the user had dragged the dialog to make it as small as
> the first button would let it. So I put a resize() command
> in on_disappear_clicked(). Unfortunately, this has no effect. The dialog
> size remains unchanged from when it had two buttons. If I manually resize
> it with my mouse, then click "show" again, it resizes to fit the second
> button, which is nice, but I want it to shrink again when the second button
> disappears.
> I want the dialog to remain resizable by the user, by the way.
>
> Can anyone tell me why this doesn't work?
>
> Thanks! This has been bugging me for weeks.
>
> #include "QMainWindow"
> #include "QPushButton"
> #include "QHBoxLayout"
> #include "QVBoxLayout"
>
> class MainWindow: public QMainWindow {
> Q_OBJECT
> public:
> MainWindow(QWidget *parent=NULL): QMainWindow(parent) {
>
> setCentralWidget(new QWidget(this));
> QVBoxLayout *layout = new QVBoxLayout;
> changer = new QPushButton("show");
> layout->addWidget(changer);
> centralWidget()->setLayout(layout);
>
> disappear = new QPushButton("disappear");
> layout->addWidget(disappear);
> disappear->hide();
> connect(changer, SIGNAL(clicked()), this, SLOT(on_changer_clicked()));
>
> connect(disappear, SIGNAL(clicked()), this,
> SLOT(on_disappear_clicked()));
> return;
> }
> private slots:
> void on_changer_clicked(){
> printf("showing...\n");
> disappear->show();
> return;
> }
> void on_disappear_clicked() {
> printf("hiding...\n");
> disappear->hide();
> resize(1,1);
> }
> public:
> QPushButton *changer;
> QPushButton *disappear;
> };
>
>
> #include "test.h"
> int main(int argc, char *argv[]) {
> QApplication app(argc, argv);
> MainWindow blah(NULL);
> blah.show();
> return app.exec();
> }
>
> -- "There's no time to stop for gas, we're already late"-- Karin Donker
> --
> Rich "wealthychef" Cook
> <http://web.mac.com/wealthychef/iWeb/Site/Urinetown.html>
> 925-784-3077
> --
>
>
>
>
Message 4 in thread
Hmm, well, your example has shown me that resize() does do something,
now I just have to find the right amount to resize by. That's
actually very helpful, thanks. If I use your code verbatim, it does
not work, but if I just say
resize(width(), height() - 10);
then the window DOES shrink by 10 pixels. So I think I'm on the road
to recovery here. :-)
I just need to find the right number to pass. Of course, this is
something that IMHO Qt should do for me, so I'll report it as a
feature request (or is it a bug?).
On Oct 3, 2006, at 9:18 AM, Glen van de Mosselaer wrote:
> Rich,
>
> Try this: adjust the window size +/- the size of your disappearing
> widget and the layout's spacing between widgets. Not perfect, but
> it's along the lines you wanted.
>
> This would probably work best if the stuff you're hiding was
> embedded in a QWidget or QFrame, or was a single custom widget.
>
>
> #include <QMainWindow>
> #include <QPushButton>
> #include <QHBoxLayout>
> #include <QVBoxLayout>
>
> class MainWindow: public QMainWindow
> {
> Q_OBJECT
> public:
> MainWindow(QWidget *parent=0): QMainWindow(parent) {
> setCentralWidget(new QWidget(this));
>
> layout = new QVBoxLayout;
>
> changer = new QPushButton("show");
> changer->setSizePolicy(QSizePolicy::Preferred,
> QSizePolicy::Preferred); // big buttons
> layout->addWidget(changer);
>
> disappear = new QPushButton("disappear");
> disappear->setSizePolicy(QSizePolicy::Preferred,
> QSizePolicy::Preferred); // big buttons
> layout->addWidget(disappear);
>
> centralWidget()->setLayout(layout);
>
> disappear->hide();
>
> connect(changer, SIGNAL(clicked()), this, SLOT
> (on_changer_clicked()));
> connect(disappear, SIGNAL(clicked()), this, SLOT
> (on_disappear_clicked()));
>
> return;
> }
>
> private slots:
> void on_changer_clicked(){
> if (!disappear->isVisible()){
> QSize addSize = disappear->size();
> resize(width(), height() + addSize.height() + layout-
> >spacing());
> disappear->show();
> }
> }
>
> void on_disappear_clicked() {
> QSize eliminateSize = disappear->size();
> disappear->hide();
> resize(width(), height() - eliminateSize.height() - layout-
> >spacing());
> }
>
> protected:
> QPushButton *changer;
> QPushButton *disappear;
> QVBoxLayout *layout;
> };
>
>
> There's an initial resize that I can't get rid of. Mucking about
> with the stretch factors an size policies didn't seem to produce
> exactly what you'd expect.
>
> Glen.
>
>
> On 10/2/06, Rich Cook <wealthychef@xxxxxxxxx> wrote:
> So, I now have a simple reproducer for a basic problem I have with Qt.
>
> The below code displays a simple dialog that has a button on it
> that shows another button if clicked. When the new button is
> clicked, it hides that button.
> I would like to tell the dialog to get smaller after hiding the
> second button, exactly as if the user had dragged the dialog to
> make it as small as the first button would let it. So I put a
> resize() command in on_disappear_clicked(). Unfortunately, this
> has no effect. The dialog size remains unchanged from when it had
> two buttons. If I manually resize it with my mouse, then click
> "show" again, it resizes to fit the second button, which is nice,
> but I want it to shrink again when the second button disappears.
> I want the dialog to remain resizable by the user, by the way.
>
> Can anyone tell me why this doesn't work?
>
> Thanks! This has been bugging me for weeks.
>
> #include "QMainWindow"
> #include "QPushButton"
> #include "QHBoxLayout"
> #include "QVBoxLayout"
>
> class MainWindow: public QMainWindow {
> Q_OBJECT
> public:
> MainWindow(QWidget *parent=NULL): QMainWindow(parent) {
>
> setCentralWidget(new QWidget(this));
> QVBoxLayout *layout = new QVBoxLayout;
> changer = new QPushButton("show");
> layout->addWidget(changer);
> centralWidget()->setLayout(layout);
>
> disappear = new QPushButton("disappear");
> layout->addWidget(disappear);
> disappear->hide();
> connect(changer, SIGNAL(clicked()), this, SLOT
> (on_changer_clicked()));
> connect(disappear, SIGNAL(clicked()), this, SLOT
> (on_disappear_clicked()));
> return;
> }
> private slots:
> void on_changer_clicked(){
> printf("showing...\n");
> disappear->show();
> return;
> }
> void on_disappear_clicked() {
> printf("hiding...\n");
> disappear->hide();
> resize(1,1);
> }
> public:
> QPushButton *changer;
> QPushButton *disappear;
> };
>
>
> #include "test.h"
> int main(int argc, char *argv[]) {
> QApplication app(argc, argv);
> MainWindow blah(NULL);
> blah.show();
> return app.exec();
> }
>
> --
> "There's no time to stop for gas, we're already late" -- Karin Donker
> --
> Rich "wealthychef" Cook
> < http://web.mac.com/wealthychef/iWeb/Site/Urinetown.html>
> 925-784-3077
> --
>
>
>
>
--
[ signature omitted ]
Message 5 in thread
On 03.10.06 11:19:08, Rich Cook wrote:
> I just need to find the right number to pass.
Just store the current size before showing the extra widget. Or fetching
the size() of the extra widget just before hiding it again plus the
spacing of the dialog.
> Of course, this is something
> that IMHO Qt should do for me, so I'll report it as a feature request (or is it
> a bug?).
Yes, a feature request.
Andreas
--
[ signature omitted ]
Message 6 in thread
I did find another angle:
resize(minimumSize());
Does most of what I want. It still allows the user to make it
smaller, which tells me there is another value somewhere I could
reduce it by, but it's progress.
On Oct 3, 2006, at 11:26 AM, Andreas Pakulat wrote:
> On 03.10.06 11:19:08, Rich Cook wrote:
>> I just need to find the right number to pass.
>
> Just store the current size before showing the extra widget. Or
> fetching
> the size() of the extra widget just before hiding it again plus the
> spacing of the dialog.
>
>> Of course, this is something
>> that IMHO Qt should do for me, so I'll report it as a feature
>> request (or is it
>> a bug?).
>
> Yes, a feature request.
>
> Andreas
>
> --
> Don't worry. Life's too long.
> -- Vincent Sardi, Jr.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx
> with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]
Message 7 in thread
The answer to this question, as I posted in another thread, is to
call QCoreApplication::sendPostedEvents () before calling resize(),
since resize() takes effect immediately, but removeWidget() posts
some events which are processed in subsequent passes through the
event loop.
On Oct 3, 2006, at 11:35 AM, Rich Cook wrote:
> I did find another angle:
> resize(minimumSize());
> Does most of what I want. It still allows the user to make it
> smaller, which tells me there is another value somewhere I could
> reduce it by, but it's progress.
>
> On Oct 3, 2006, at 11:26 AM, Andreas Pakulat wrote:
>
>> On 03.10.06 11:19:08, Rich Cook wrote:
>>> I just need to find the right number to pass.
>>
>> Just store the current size before showing the extra widget. Or
>> fetching
>> the size() of the extra widget just before hiding it again plus the
>> spacing of the dialog.
>>
>>> Of course, this is something
>>> that IMHO Qt should do for me, so I'll report it as a feature
>>> request (or is it
>>> a bug?).
>>
>> Yes, a feature request.
>>
>> Andreas
>>
>> --
>> Don't worry. Life's too long.
>> -- Vincent Sardi, Jr.
>>
>> --
>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx
>> with "unsubscribe" in the subject or the body.
>> List archive and information: http://lists.trolltech.com/qt-interest/
>
> --
> "There's no time to stop for gas, we're already late"-- Karin Donker
> --
> Rich "wealthychef" Cook
> <http://web.mac.com/wealthychef/iWeb/Site/Urinetown.html>
> 925-784-3077
> --
>
>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx
> with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]