Qt学习笔记(Layout)

#-------------------------------------------------
#
# Project created by QtCreator 2019-08-16T17:49:14
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = 04_Layout
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"

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

MainWindow:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用PyQt Designer可以方便地实现嵌套布局,如网格布局、水平布局、垂直布局等。下面是一个简单的示例,用于演示如何使用PyQt Designer进行布局: 1. 打开PyQt Designer,选择“Main Window”模板,创建一个新的窗口。 2. 在窗口中添加一个QVBoxLayout布局,用于垂直排列组件。 3. 在QVBoxLayout布局中添加一个QHBoxLayout布局,用于水平排列组件。 4. 在QHBoxLayout布局中添加一个QLabel和一个QLineEdit组件。 5. 在QVBoxLayout布局中添加一个QTextEdit组件。 6. 在QVBoxLayout布局中添加一个QPushButton组件,并将其设置为居中对齐。 7. 保存并运行该窗口,查看布局效果。 以下是示例代码,您可以在PyQt Designer中打开.ui文件并通过拖放组件来创建布局: ```xml <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout"> <item> <layout class="QHBoxLayout" name="horizontalLayout"> <item> <widget class="QLabel" name="label"> <property name="text"> <string>Label:</string> </property> </widget> </item> <item> <widget class="QLineEdit" name="lineEdit"/> </item> </layout> </item> <item> <widget class="QTextEdit" name="textEdit"/> </item> <item> <spacer name="verticalSpacer"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>0</width> <height>40</height> </size> </property> <property name="sizePolicy"> <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> </spacer> </item> <item> <widget class="QPushButton" name="pushButton"> <property name="text"> <string>Button</string> </property> <property name="sizePolicy"> <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="layoutDirection"> <enum>Qt::LeftToRight</enum> </property> <property name="autoDefault"> <bool>true</bool> </property> <property name="default"> <bool>true</bool> </property> <property name="flat"> <bool>false</bool> </property> <property name="checkable"> <bool>false</bool> </property> <property name="autoRepeat"> <bool>false</bool> </property> <property name="autoExclusive"> <bool>false</bool> </property> <property name="default"> <bool>false</bool> </property> <property name="autoRepeatDelay"> <number>0</number> </property> <property name="autoRepeatInterval"> <number>0</number> </property> </widget> </item> </layout> </widget> </widget> <resources/> <connections/> </ui> ``` 注意:为了使QPushButton组件在布局中居中对齐,我们需要手动设置它的属性。在上面的示例中,我们将QPushButton的“sizePolicy”属性的“vsizetype”设置为“Fixed”,并将“sizeHint”调整为合适的高度值(这里设置为40)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值