2017-04-03 46 views
-2

私の学位のプロジェクトで働いていて、GUIに問題があります。Qt:setGeometryがアプリケーションの実行時にサイズ変更されない

私は試したコードを単純化するために、別々のプロジェクトで異なるウィジェットの配置をテストしました。

ここに私の問題があります: すべての私のウィジェットのサイズを変更するためにオーバーロードされたresizeEventを作成しました。

私のテストプログラムの起動後、ウィジェットのサイズがあまり大きくないので、setGeometryコールが入っていても、ウィンドウを手動でサイズ変更して強制的にリサイズを強制する必要があります。次のコードのようなコンストラクタです。ウィジェットはよく始まり

UPDATEからサイズであるように、私は解決策を見つけようとしています.H

MainWindow.h 
------------- 
#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 
#include <QPushButton> 
#include <QGridLayout> 
#include <QGroupBox> 

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 
    void resizeEvent (QResizeEvent * event); 

private: 
    Ui::MainWindow *ui; 

    QGroupBox *g1; 
    QGroupBox *g2; 
    QPushButton *but2; 
    QGridLayout *lay; 
}; 

#endif // MAINWINDOW_H 

た.cpp

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 

    this->lay = new QGridLayout(); 
    this->g1 = new QGroupBox("g1"); 
    this->g2 = new QGroupBox("g2"); 
    this->but2 = new QPushButton("But2"); 
    this->lay->addWidget(g1, 0, 0, 1, 1); 
    this->lay->addWidget(but2, 0, 1, 1, 1); 
    this->lay->addWidget(g2, 1, 0, 1, 2); 
    this->g1->setBaseSize(60, 60); 
    this->g2->setBaseSize(60, 60); 

    this->ui->centralWidget->setLayout(lay); 
    this->ui->centralWidget->setMinimumSize(100, 100); 

    this->but1->setHidden(true); 

    this->g1->setGeometry(5, 1, (this->ui->centralWidget->width())55, (this->ui->centralWidget->height())-83); 
    this->but2->setGeometry(this->g1->width()+10 , 6, 40, (this->ui->centralWidget->height())-88); 
    this->g2->setGeometry(5, this->g1->height()+5, (this->ui->centralWidget->width())-10, 70); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

void MainWindow::resizeEvent (QResizeEvent * event) 
{ 
    this->g1->setGeometry(5, 1, (this->ui->centralWidget->width())-55, (this->ui->centralWidget->height())-89); 
    this->but2->setGeometry(this->g1->width()+10 , 6, 40, (this->ui->centralWidget->height())-88); 
    this->g2->setGeometry(5, this->g1->height()+5, (this->ui->centralWidget->width())-10, 70); 
} 

ここに2枚の写真があります:

前に、私は、後に問題は内部の何もないとき、QGroupBoxはちょうど隣にあるボタンのサイズを取ることです

after resize

のサイズを変更

before resize

のサイズを変更最初から大きくしたいです

+1

「resizeEvent」を上書きしないことを強くお勧めします。大きなプロジェクトでこれを見て、あらゆる種類の問題を引き起こしました。代わりに、通常は可能で、最後にははるかにスムーズなレイアウトとスペーサーを使用して、完全に目的の外観を達成しようとします。あなたが初期の見た目のスクリーンショットを含めるなら、誰かがあなたを助けることができるかもしれません。 – Bowdzone

+0

私は同意するが、ここでの問題はresizeeventだとは思わない。なぜなら、ウィンドウのサイズを変更するとうまくいくからだ。私のdifferentsレイアウトのサイズは、それらのどれが隠されているか表示されているかによって異なります。私がここにある問題は、アプリケーションの起動時に、setGeometryがコンストラクタの最後であっても実行されないように見えるということです。 QCoreApplication :: processEventやwidget.updateのようないくつかのウィジェットをウィンドウに追加しようとしましたが、何も起こりません。 –

+0

はい、それでもXY問題です。ウィジェットのジオメトリは他のウィジェットに依存しています。つまり、サイズを変更する前に一度ペイントする必要があります。だから私はあなたの頭痛の少ない与える必要がこの周りの別の方法を見つけるだろう。 – Bowdzone

答えて

0

setGeometryの代わりに、(あまりにも)(そしておそらくresizeEventで)他のウィジェットのような...

int col = 0, row = 0; 
int stretch = 100 * (this->ui->centralWidget->width()-55.0)/this->ui->centralWidget->width(); 
this->lay->setColumnStretch(col, stretch); 
stretch = 100 * (this->ui->centralWidget->height())-89.0)/this->ui->centralWidget->height(); 
this->lay->setRowStretch(row, stretch); 

同じようにレイアウトにウィジェットを追加する際、LDストレッチ係数を使用。

関連する問題