2017-04-03 3 views
-2

저는 학위를 수여하는 프로젝트에서 일하고 있는데 GUI에 문제가 있습니다.Qt : 앱 실행시 setGeometry의 크기가 변경되지 않습니다.

내가 시도한 코드를 단순화하기 위해 분리 된 프로젝트에서 다른 위젯의 배치를 테스트했습니다.

여기 내 문제가 있습니다 : 모든 위젯의 크기를 조정하려면 오버로드 된 resizeEvent을 만들었습니다. 그것은 한가지를 제외하고 위대한 일을합니다 :

내 테스트 프로그램을 시작한 후에 위젯의 크기가 적당하지 않아 setGeometry 호출이있는 경우에도 재구성을 강제 실행하기 위해 한 번 수동으로 창 크기를 조정해야합니다. 다음 코드와 같은 생성자.

.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 

통화 당

나는 위젯이 아니라 처음부터 크기가되도록 해결책을 찾기 위해 노력하고
#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); 
} 

UPDATE 다음은 두 그림입니다 :

전에 나는, 후

after resize

문제는 아무것도 내부에 존재하지 않는 경우는 QGroupBox 그냥 옆에있는 버튼의 크기를 가지고 있다는 것입니다 크기를 조정

before resize

의 크기를 조정 처음부터 더 크게하고 싶습니다.

+1

나는'resizeEvent'를 덮어 쓰지 말 것을 강력하게 제안했다. 큰 프로젝트에서 이것을 보았고, 모든 종류의 문제를 일으켰다. 대신 레이아웃과 스페이서를 통해 원하는 모양을 완성 시키십시오. 일반적으로 가능하고 훨씬 부드럽게 끝납니다. 초기 및 원하는 모습의 스크린 샷을 포함 시키면 다른 사람도 도움을 줄 수 있습니다. – Bowdzone

+0

나는 동의하지만 여기는 문제가 내 크기를 조정할 때 그것은 잘 작동 resizeevent 이유라고 생각하지 않습니다. 내 differents 레이아웃의 크기가 숨겨져 있거나 볼 수있는 설정에 따라 달라지기 때문에 나는 그것을 과대 평가했다. 내가 여기있는 문제는 그 응용 프로그램의 시작에 setGeometry가 생성자의 끝 인 경우에도 실행되지 않는다는 것입니다. QCoreApplication :: processEvent 나 widget.update와 같은 줄을 창에 추가하려고했지만 아무 것도 나타나지 않습니다. –

+0

예.하지만 여전히 XY 문제입니다. 위젯의 기하학은 다른 위젯에 달려 있습니다. 즉, 내가 생각하기에 크기를 조정하기 전에 모든 것을 한 번 그려야한다는 것을 의미합니다. 그래서 나는이 문제를 해결할 다른 방법을 찾아 낼 것이고, 두통이 줄어들 것입니다. – Bowdzone

답변

0

setGeometry 대신에 다른 위젯 ... 같은

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); 

같은 방법으로 레이아웃에 위젯을 추가 (그리고 어쩌면 resizeEvent에서()도) 때 신분증은 스트레치 요소를 사용합니다.