2013-04-17 2 views
0

나는 QT에서 새롭다. 각 라인에 2 개의 버튼, 2 개의 라디오 버튼과 2 개의 랄프가있는 1024 line 프로그램을 개발합니다. 나는 그것을 위해 두 가지 방법이 있습니다. 내가 드래그 2 개 * 1204 버튼, 2 개 * 1024 라벨하고 비논리적 인 2 개 * 1024 라디오 버튼을 드롭 디자이너 모드에서디자인하지 않고 페이지에 Qbutton을 추가하는 방법은 무엇입니까?

  1. .
  2. 디자이너 모드 및 드래그 앤 드롭없이이 위젯을 페이지에 추가하는 방법이 있습니다. 예를 들어 런타임에 단추를 클릭하고 코드 숨김 이 위젯 (레이블, 단추, 라디오 단추)을 추가하십시오. 페이지 또는이 같은 것을 가리 킵니다.

나는 웹 프로그래밍에서 두 번째 방법을 사용했습니다. QT에서 가능한가요? 아니면 그런거야?

답변

0

위젯을 프로그래밍 방식으로 만들고 레이아웃을 통해 위치를 조정할 수 있습니다. 예를 들어, 지금과 같을 수 있습니다 당신이 재생할 수있는 레이아웃 (등 QBoxLayout, QGridLayout, QFormLayout)의 다른 종류가 있습니다

QVBoxLayout *topLayout = new QVBoxLayout(); 

for (int lineNumber = 0; lineNumber < 1024; ++lineNumber) 
{ 
    QWidget *oneLineWidget = new QWidget(this); 
    QHBoxLayout *oneLineWidgetLayout = new QHBoxLayout(); 
    { //added these brackets just for the ease of reading. 
     QLabel *labFirst = new QLabel(tr("first label"), oneLineWidget); 
     QLabel *labSecond = new QLabel(tr("second label"), oneLineWidget); 
     QPushButton *bFirst = new QPushButton(tr("first button"), oneLineWidget); 
     QPushButton *bSecond = new QPushButton(tr("second button"), oneLineWidget); 
     QRadioButton *rbFirst = new QRadioButton(tr("first radiobutton"), oneLineWidget); 
     QRadioButton *rbSecond = new QRadioButton(tr("second radiobutton"), oneLineWidget); 

     oneLineWidgetLayout->addWidget(labFirst); 
     oneLineWidgetLayout->addWidget(labSecond); 
     oneLineWidgetLayout->addWidget(bFirst); 
     oneLineWidgetLayout->addWidget(bSecond); 

     //lets put one radioButton under another. 
     QVBoxLayout *radioButtonsLayout = new QVBoxLayout(); 
     { 
      radioButtonsLayout->addWidget(rbFirst); 
      radioButtonsLayout->addWidget(rbSecond); 
     } 
     //and now we can combine layouts. 
     oneLineWidgetLayout->addLayout(radioButtonsLayout); 

    } 
    oneLineWidget->setLayout(oneLineWidgetLayout); 

    topLayout->addWidget(oneLineWidget); 
} 

this->setLayout(topLayout); 

. QLayout documentation부터 시작할 수 있습니다. 클래스를 상속하는 클래스 목록이 있습니다. 도움이되기를 바랍니다. :) 행운을 빕니다!

+0

P. 나는 그것을 메모장에 썼고 "실생활에서"시험하지 않았다. 그래서 아마도 약간의 오류가 있습니다. 그러나 적어도 그것이 올바른 방향으로 당신을 가리킬 것이기를 바랍니다. – FreeNickname

0

Qt Designer에서하는 모든 작업은 결국 객체를 만들고 서로 링크하는 C++ 코드로 변환됩니다.

그래서 생성 된 헤더 파일을보고 해당 코드를 for 루프에 넣는 것은 매우 간단합니다. 또는 처음부터 다시 시작하여 세 개의 버튼을 만들고 레이아웃에 추가하는 것은 몇 줄의 코드 일뿐입니다.

관련 문제