如何自动调整qt小部件的大小?

发布于 2024-08-17 22:39:05 字数 4268 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念满溢 2024-08-24 22:39:05

您可以尝试

mainLayout->activate();

使用changeString()强制重做布局。 (不过,您必须使 mainLayout 成为类成员,或者通过 layout() 获取布局)。

另外,尝试添加拉伸因子为零的控件:

mainLayout->addWidget(strEntries[i], 0);

You could try

mainLayout->activate();

in changeString(). That forces the layout to be redone. (You'll have to make mainLayout a class member though, or alternatively get the layout through layout()).

Also, try adding the controls with a stretch factor of zero:

mainLayout->addWidget(strEntries[i], 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文