如何自动调整qt小部件的大小?
QT版本:4.5.2 操作系统:ubuntu 8.04
我在自动调整小部件大小方面遇到问题,这里是一个示例:
alt text http://img15.imageshack.us/img15/3479/autoresize.png
如图所示,这里有两个 widget,一个是“StrWidget”(图 1 中上方的 widget),它有 5 个QLabels 用于显示字符串,另一个是“CtrlWidget”,它告诉 StrWidget 显示什么字符串。
当我点击“send CCC”时,StrWidget就变成了图2所示的widget,
但是当我再次点击“send A”时,StrWidget就变成了图3所示的widget。
有一个成员函数StrWidget::changeString,它是一个槽StrWidget并接收用于显示的字符串。
void
StrWidget::changeString(QString inputStr)
{
for(int i=0;i<5;i++){
strEntries[i]->setText(inputStr);
}
}
当调用changeString 时,StrWidget 无法自动调整自身大小。我尝试以下两种方法:
(1)在StrWidget::changeString中插入“adjustSize()”,它可以工作,但我需要单击按钮两次才能调整大小。我不知道为什么会这样。
(2) 插入“隐藏();显示();”在 StrWidget::changeString 中,它可以工作,但是当我单击按钮时 StrWidget 会刷新。
有人对此有什么想法吗?谢谢~
以下是示例
main的源代码:
#include "StrWidget.h"
#include "CtrlWidget.h"
#include <QApplication>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
StrWidget *strWidget=new StrWidget(0);
CtrlWidget *ctrlWidget=new CtrlWidget(0);
strWidget->show();
ctrlWidget->show();
QObject::connect(ctrlWidget,SIGNAL(sendString(QString)),strWidget,SLOT(changeString(QString)));
int ref=app.exec();
strWidget->deleteLater();
ctrlWidget->deleteLater();
return ref;
}
StrWidget.h
#include <QWidget>
#include <QString>
#include <QLabel>
#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
StrWidget.cpp
#include "StrWidget.h"
#include <QPushButton>
#include <QHBoxLayout>
StrWidget::StrWidget(QWidget *parent):QWidget(parent)
{
//main layout
QHBoxLayout *mainLayout=new QHBoxLayout(this);
setLayout(mainLayout);
//info label setup
for(int i=0;i<5;i++){
strEntries[i]=new QLabel(this);
strEntries[i]->setText("A");
strEntries[i]->setFrameShape(QFrame::StyledPanel);
mainLayout->addWidget(strEntries[i]);
}
//clearBtn setup
QPushButton *clearBtn=new QPushButton(tr("Clear Str"),this);
connect(clearBtn,SIGNAL(clicked()),this,SLOT(clearString()));
mainLayout->addWidget(clearBtn);
}
void
StrWidget::changeString(QString inputStr)
{
for(int i=0;i<5;i++){
strEntries[i]->setText(inputStr);
}
}
void
StrWidget::clearString()
{
changeString("");
}
CtrlWidget.h
#include <QWidget>
#include <QString>
#ifndef _CtrlWidget_H_
#define _CtrlWidget_H_
class CtrlWidget:public QWidget
{
Q_OBJECT
public:
CtrlWidget(QWidget *parent=0);
signals:
void sendString(QString sentStr);
};
CtrlWidget.cpp
#include "CtrlWidget.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QSignalMapper>
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->addWidget(sendABtn);
mainLayout->addWidget(sendBBBtn);
mainLayout->addWidget(sendCCCBtn);
//map setup
QSignalMapper *btnMapper=new QSignalMapper(this);
btnMapper->setMapping(sendABtn,"A");
btnMapper->setMapping(sendBBBtn,"BB");
btnMapper->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()));
}
QT Version: 4.5.2
OS: ubuntu 8.04
I have a trouble with auto-resizing widget, here is an example:
alt text http://img15.imageshack.us/img15/3479/autoresize.png
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.
When I click "send CCC", the StrWidget becomes the widget shown in figure 2
But when I click "send A" again, the StrWidget becomes the widget shown in figure 3
There is a member function StrWidget::changeString which is a slot of StrWidget and receive the string for showing.
void
StrWidget::changeString(QString inputStr)
{
for(int i=0;i<5;i++){
strEntries[i]->setText(inputStr);
}
}
the StrWidget cannot auto-resize itself when changeString is called. I try the following two methods:
(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.
(2) Insert "hide();show();" in StrWidget::changeString, it works but the StrWidget would flush when I click buttons.
Dose anyone have an idea about it? Thanks~
The following is the source code of the example
main:
#include "StrWidget.h"
#include "CtrlWidget.h"
#include <QApplication>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
StrWidget *strWidget=new StrWidget(0);
CtrlWidget *ctrlWidget=new CtrlWidget(0);
strWidget->show();
ctrlWidget->show();
QObject::connect(ctrlWidget,SIGNAL(sendString(QString)),strWidget,SLOT(changeString(QString)));
int ref=app.exec();
strWidget->deleteLater();
ctrlWidget->deleteLater();
return ref;
}
StrWidget.h
#include <QWidget>
#include <QString>
#include <QLabel>
#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
StrWidget.cpp
#include "StrWidget.h"
#include <QPushButton>
#include <QHBoxLayout>
StrWidget::StrWidget(QWidget *parent):QWidget(parent)
{
//main layout
QHBoxLayout *mainLayout=new QHBoxLayout(this);
setLayout(mainLayout);
//info label setup
for(int i=0;i<5;i++){
strEntries[i]=new QLabel(this);
strEntries[i]->setText("A");
strEntries[i]->setFrameShape(QFrame::StyledPanel);
mainLayout->addWidget(strEntries[i]);
}
//clearBtn setup
QPushButton *clearBtn=new QPushButton(tr("Clear Str"),this);
connect(clearBtn,SIGNAL(clicked()),this,SLOT(clearString()));
mainLayout->addWidget(clearBtn);
}
void
StrWidget::changeString(QString inputStr)
{
for(int i=0;i<5;i++){
strEntries[i]->setText(inputStr);
}
}
void
StrWidget::clearString()
{
changeString("");
}
CtrlWidget.h
#include <QWidget>
#include <QString>
#ifndef _CtrlWidget_H_
#define _CtrlWidget_H_
class CtrlWidget:public QWidget
{
Q_OBJECT
public:
CtrlWidget(QWidget *parent=0);
signals:
void sendString(QString sentStr);
};
CtrlWidget.cpp
#include "CtrlWidget.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QSignalMapper>
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->addWidget(sendABtn);
mainLayout->addWidget(sendBBBtn);
mainLayout->addWidget(sendCCCBtn);
//map setup
QSignalMapper *btnMapper=new QSignalMapper(this);
btnMapper->setMapping(sendABtn,"A");
btnMapper->setMapping(sendBBBtn,"BB");
btnMapper->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()));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试
使用
changeString()
。 强制重做布局。 (不过,您必须使mainLayout
成为类成员,或者通过layout()
获取布局)。另外,尝试添加拉伸因子为零的控件:
You could try
in
changeString()
. That forces the layout to be redone. (You'll have to makemainLayout
a class member though, or alternatively get the layout throughlayout()
).Also, try adding the controls with a stretch factor of zero: