QT布局管理器问题
//mainwindow.cpp#include"mainwindow.h"#include<QGridLayout>MainWindow::MainWindow(QWi...
//mainwindow.cpp
#include "mainwindow.h"#include <QGridLayout>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ label1=new QLabel(this); label1->setText(tr("请输入半径: ")); lineEdit=new QLineEdit(this); label2=new QLabel(this); button=new QPushButton(this); button->setText(tr("the circle area")); QGridLayout *mainLayout=new QGridLayout(this); mainLayout->addWidget(label1,0,0); mainLayout->addWidget(lineEdit,0,1); mainLayout->addWidget(label2,1,0); mainLayout->addWidget(button,1,1);//setLayout(mainLayout)
}
//mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QLabel>#include <QLineEdit>#include <QPushButton>class MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow(QWidget *parent = 0); // ~MainWindow();private: QLabel *label1,*label2; QLineEdit *lineEdit; QPushButton *button;};#endif // MAINWINDOW_H
为什么控件都挤在一坨了 展开
#include "mainwindow.h"#include <QGridLayout>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ label1=new QLabel(this); label1->setText(tr("请输入半径: ")); lineEdit=new QLineEdit(this); label2=new QLabel(this); button=new QPushButton(this); button->setText(tr("the circle area")); QGridLayout *mainLayout=new QGridLayout(this); mainLayout->addWidget(label1,0,0); mainLayout->addWidget(lineEdit,0,1); mainLayout->addWidget(label2,1,0); mainLayout->addWidget(button,1,1);//setLayout(mainLayout)
}
//mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QLabel>#include <QLineEdit>#include <QPushButton>class MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow(QWidget *parent = 0); // ~MainWindow();private: QLabel *label1,*label2; QLineEdit *lineEdit; QPushButton *button;};#endif // MAINWINDOW_H
为什么控件都挤在一坨了 展开
2个回答
展开全部
mainlayout本身没有放好,给你一段代码,你新建一个qt creator 普通的界面项目看看,很简单。
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget * pWid = new QWidget(this);
QGridLayout * pLo = new QGridLayout(this);
QPushButton * pBtn001 = new QPushButton(NULL);
pBtn001->setText("btn001");
QPushButton * pBtn002 = new QPushButton(NULL);
pBtn002->setText("btn002");
QPushButton * pBtn003 = new QPushButton(NULL);
pBtn003->setText("btn003");
QMainWindow::setCentralWidget(pWid);
pWid->setLayout(pLo);
QSizePolicy spBtn(QSizePolicy::Minimum, QSizePolicy::Minimum);
spBtn.setHorizontalStretch(0);
spBtn.setVerticalStretch(0);
pBtn001->setSizePolicy(spBtn);
pBtn002->setSizePolicy(spBtn);
pBtn003->setSizePolicy(spBtn);
pLo->setColumnStretch(0,0);
pLo->setColumnStretch(1,0);
pLo->addWidget(pBtn001,0,0,1,1);
pLo->addWidget(pBtn002,0,1,2,1);
pLo->addWidget(pBtn003,1,0,1,1);
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget * pWid = new QWidget(this);
QGridLayout * pLo = new QGridLayout(this);
QPushButton * pBtn001 = new QPushButton(NULL);
pBtn001->setText("btn001");
QPushButton * pBtn002 = new QPushButton(NULL);
pBtn002->setText("btn002");
QPushButton * pBtn003 = new QPushButton(NULL);
pBtn003->setText("btn003");
QMainWindow::setCentralWidget(pWid);
pWid->setLayout(pLo);
QSizePolicy spBtn(QSizePolicy::Minimum, QSizePolicy::Minimum);
spBtn.setHorizontalStretch(0);
spBtn.setVerticalStretch(0);
pBtn001->setSizePolicy(spBtn);
pBtn002->setSizePolicy(spBtn);
pBtn003->setSizePolicy(spBtn);
pLo->setColumnStretch(0,0);
pLo->setColumnStretch(1,0);
pLo->addWidget(pBtn001,0,0,1,1);
pLo->addWidget(pBtn002,0,1,2,1);
pLo->addWidget(pBtn003,1,0,1,1);
}
更多追问追答
追问
那个~我现在想要通过代码形式来实现界面布局,不通过自带的界面设计来实现
ui(new Ui::MainWindow) //这样子是不对的吧
追答
嗯,不是全部代码都有效,但是关于按钮布局这块是有效的,你只要针对这块复制黏贴就行了吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询