QT之setSizePolicy与setStretchFactor



class MyTestMain : public QWidget
{
public:
    MyTestMain(QWidget* pParent = NULL) :QWidget(pParent)
    {
        QHBoxLayout* pLayout = new QHBoxLayout(this);
        QTextEdit* pEditUp = new QTextEdit(QObject::tr("Edit up"));
        pEditUp->SetAlignment(Qt::AlignCenter);

        pLayout->AddWidget(pEditUp);

// 控件在布局中,设置才有效。两个参数分别是水平和竖直扩展性,此处设置为水平扩展,竖直固定。默认都是可扩展

        pEditUp->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::fixed);

        QTextEdit* pEditBottom = new QTextEdit(QObject::tr("Edit Bottom"));
        pEditBottom->SetAlignment(Qt::AlignCenter);

        pLayout->AddWidget(pEditUp);

//设置控件可扩展(>0可扩展),此处是水平布局,默认扩展是在竖直方向,该处设置的就是水平方向,如果要设置竖直方向就用setSizePolicy

// 竖直布局与之类似

        pLayout->setStretchFactor(pEditUp, 1);
        pLayout->setStretchFactor(pEditBottom, 1);
    }
    ~MyTestMain()
    {}
};


void TestLayout(int argc, char* argv[])
{
    QApplication a(argc, argv);
    MyTestMain w;
    w.show();
    a.exec();
}


int main(int argc, char* argv[])
{
    TestLayout();
    return 0;
}


#ifdef _MSC_VER
#include <windows.h>
int WINAPI WinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, 
__in_opt LPSTR lpCmdLine, __in int nShowCmd)
{
    return main(__argc, __argv);
}

#endif



展开阅读全文

没有更多推荐了,返回首页