当前位置: 首页 > news >正文

大连网站建设蛇皮果西安网站seo技术厂家

大连网站建设蛇皮果,西安网站seo技术厂家,网站做seo多少钱,怎么把自己的网站放到百度搜索上目录 qt下载地址: widget 宽高 管理信息列表源码 c版: pro文件: qt 设置水平布局,里面有两个按钮,每个按钮就变的很宽,怎么设置按钮的精确位置 设置固定大小: 使用弹性空间(…

目录

qt下载地址:

widget 宽高

管理信息列表源码

c++版:

pro文件:

qt 设置水平布局,里面有两个按钮,每个按钮就变的很宽,怎么设置按钮的精确位置

设置固定大小:

使用弹性空间(Spacer)

使用布局比例:

qt c++ 加载ui文件:

方法1: 使用Qt Designer UI文件直接加载

方法2: 将UI文件转换为C++代码


qt下载地址:

Download Qt: Install and get started

widget 宽高

管理信息列表源码

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QListWidget, QListWidgetItem, QLabel, QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QSizeclass MainWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("Student Information List")self.setGeometry(100, 100, 450, 400)self.listWidget = QListWidget(self)self.setCentralWidget(self.listWidget)# 示例数据students = [{"name": "John Doe", "id": "123456789", "gender": "Male", "age": "20", "height": "180cm", "avatar": "res/drawable/head.png"},{"name": "Jane Smith", "id": "987654321", "gender": "Female", "age": "22", "height": "170cm", "avatar": "res/drawable/head.png"},{"name": "aaaa", "id": "fsadf", "gender": "sfd", "age": "sdf", "height": "170cm", "avatar": "res/drawable/head.png"},# 添加更多学生数据]for student in students:self.add_student(student)def add_student(self, student):# 创建一个自定义的QWidgetwidget = QWidget()hbox = QHBoxLayout()hbox.setContentsMargins(10, 0, 10, 0)hbox.setSpacing(5);# 头像label_avatar = QLabel()pixmap = QPixmap(student["avatar"]).scaled(75, 82)  # 假设头像图片的路径正确label_avatar.setPixmap(pixmap)label_avatar.setFixedSize(80, 90)hbox.addWidget(label_avatar)# 其他信息info_widget = QWidget()vbox = QVBoxLayout()# 第一行信息:名字和身份证号label_name_id = QLabel(f"姓名:{student['name']} 性别:{student['gender']}, {student['id']}")label_name_id.setFixedSize(300, 20)vbox.addWidget(label_name_id)# 第二行信息:性别,年龄,身高label_details = QLabel(f"类型:{student['age']}  单位:{student['age']} years old, {student['height']}")label_details.setFixedSize(300, 20)vbox.addWidget(label_details)# 第二行信息:性别,年龄,身高label_details = QLabel(f"职务:{student['age']}  证件号:{student['age']} years old, {student['height']}")label_details.setFixedSize(300, 20)vbox.addWidget(label_details)vbox.setContentsMargins(10, 10, 10, 10)vbox.setSpacing(5);info_widget.setLayout(vbox)hbox.addWidget(info_widget)widget.setLayout(hbox)# 将自定义widget加入到QListWidgetItem中item = QListWidgetItem()item.setSizeHint(widget.sizeHint())  # 必须设置sizeHintself.listWidget.addItem(item)self.listWidget.setItemWidget(item, widget)if __name__ == '__main__':app = QApplication(sys.argv)window = MainWindow()window.show()sys.exit(app.exec_())

c++版:

main.cpp

#include <QApplication>
#include <QMainWindow>
#include <QListWidget>
#include <QListWidgetItem>
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPixmap>// 示例数据
struct Student {QString name;QString id;QString gender;QString age;QString height;QString avatar;
};class MainWindow : public QMainWindow {
public:MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {setWindowTitle("Student Information List");setGeometry(100, 100, 450, 400);auto listWidget = new QListWidget(this);setCentralWidget(listWidget);QVector<Student> students = {{"John Doe", "123456789", "Male", "20", "180cm", "111.jpg"},{"Jane Smith", "987654321", "Female", "22", "170cm", "111.jpg"},{"aaaa", "fsadf", "sfd", "sdf", "170cm", "111.jpg"}// 添加更多学生数据};for (auto &student : students) {addStudent(student, listWidget);}}void addStudent(const Student &student, QListWidget *listWidget) {auto widget = new QWidget();auto hbox = new QHBoxLayout(widget);hbox->setContentsMargins(10, 0, 10, 0);hbox->setSpacing(5);// 头像auto labelAvatar = new QLabel();QPixmap pixmap(student.avatar);labelAvatar->setPixmap(pixmap.scaled(75, 82));labelAvatar->setFixedSize(80, 90);hbox->addWidget(labelAvatar);// 其他信息auto infoWidget = new QWidget();auto vbox = new QVBoxLayout(infoWidget);// 第一行信息:名字和身份证号auto labelNameId = new QLabel(QString("姓名:%1 性别:%2, %3").arg(student.name, student.gender, student.id));labelNameId->setFixedSize(300, 20);vbox->addWidget(labelNameId);// 第二行信息:年龄,身高auto labelDetails = new QLabel(QString("年龄:%1 years old, 身高:%2").arg(student.age, student.height));labelDetails->setFixedSize(300, 20);vbox->addWidget(labelDetails);vbox->setContentsMargins(10, 10, 10, 10);vbox->setSpacing(5);hbox->addWidget(infoWidget);auto item = new QListWidgetItem();item->setSizeHint(widget->sizeHint());listWidget->addItem(item);listWidget->setItemWidget(item, widget);}
};int main(int argc, char *argv[]) {QApplication app(argc, argv);MainWindow window;window.show();return app.exec();
}

pro文件:

QT       += core gui
QT += core gui widgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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 it uses 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.0SOURCES += \main.cppHEADERS += \mainwindow.hFORMS += \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

qt 设置水平布局,里面有两个按钮,每个按钮就变的很宽,怎么设置按钮的精确位置

  • 设置固定大小

  • 可以为按钮设置固定的宽度和高度,这样按钮就不会根据布局自动调整大小了。

QPushButton *button1 = new QPushButton("Button 1");
QPushButton *button2 = new QPushButton("Button 2");button1->setFixedSize(100, 30); // 设置按钮的固定大小
button2->setFixedSize(100, 30);QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
  • 使用弹性空间(Spacer)

  • : 你可以在按钮之间或按钮周围添加空间,这样可以更精确地控制按钮的位置。

QPushButton *button1 = new QPushButton("Button 1");
QPushButton *button2 = new QPushButton("Button 2");QHBoxLayout *layout = new QHBoxLayout;
layout->addStretch(1); // 在布局开始处添加弹性空间
layout->addWidget(button1);
layout->addStretch(1); // 在两个按钮之间添加弹性空间
layout->addWidget(button2);
layout->addStretch(1); // 在布局结束处添加弹性空间
  • 使用布局比例

  • 使用 QSizePolicy 来设置控件的大小策略,允许更细致的控制。

QPushButton *button1 = new QPushButton("Button 1");
QPushButton *button2 = new QPushButton("Button 2");button1->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
button2->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1, 1);
layout->addWidget(button2, 1);

qt c++ 加载ui文件:

方法1: 使用Qt Designer UI文件直接加载

在这种方法中,你可以使用QUiLoader类来动态加载UI文件。这种方式的优点是你可以在不重新编译程序的情况下更改UI设计,但缺点是可能会增加程序的启动时间和复杂性。

  1. 包括必要的头文件

    #include <QUiLoader> 
    #include <QFile> 
    #include <QWidget>
    
  2. 加载.ui文件: 你可以创建一个函数来加载UI文件,并返回一个指向加载的界面的指针。

    QWidget* loadUiFile(QWidget* parent)
    {QFile file(":/path/to/your.ui");file.open(QFile::ReadOnly);QUiLoader loader;QWidget* formWidget = loader.load(&file, parent);file.close();return formWidget;
    }
    

  3. 在主窗口中使用加载的UI

#include <QApplication>
#include <QVBoxLayout>int main(int argc, char *argv[])
{QApplication app(argc, argv);QWidget window;QWidget* myForm = loadUiFile(&window);QVBoxLayout* layout = new QVBoxLayout;layout->addWidget(myForm);window.setLayout(layout);window.show();return app.exec();
}

方法2: 将UI文件转换为C++代码

Qt提供了一个名为uic的工具,它可以将UI文件转换为C++类。这种方法的优点是执行效率更高,因为UI直接编译到程序中,但缺点是每次UI改变都需要重新编译。

  1. 在.pro文件中添加UI文件: 将UI文件添加到Qt项目文件中以自动调用uic工具。

    FORMS += mainwindow.ui
    
  2. 使用转换后的类uic工具会生成一个头文件,通常命名为ui_<filename>.h,你可以在你的C++类中包含这个文件,并使用其中的类。

#include "ui_mainwindow.h"class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr){ui.setupUi(this);}private:Ui::MainWindow ui;
};
  1. 在主函数中创建和显示窗口

int main(int argc, char *argv[])
{QApplication app(argc, argv);MainWindow mainWindow;mainWindow.show();return app.exec();
}

这两种方法可以根据不同的开发需求选择使用。如果需要灵活性和频繁的UI更改,推荐使用方法1;如果追求性能和稳定性,推荐使用方法2。

http://www.hkea.cn/news/972161/

相关文章:

  • 学编程的孩子有什么好处seo网站诊断文档案例
  • 广州中新知识城开发建设网站无锡百姓网推广
  • 宝鸡做网站费用关键词你们懂的
  • wordpress 仿站 教程百度竞价点击一次多少钱
  • 做h的游戏 迅雷下载网站百度推广管家
  • 营销型网站建设的目的外贸网站平台都有哪些 免费的
  • 广东做网站公司广州从化发布
  • 能发外链的网站国际新闻今天最新消息
  • 做软件的网站关键词优化快速排名
  • 网站建设与管理简介网站链接交易
  • 英文网站建设教程网盘资源搜索神器
  • 做旅游网站的引言最新网络推广平台
  • 服务器上给网站做301跳转企业网站注册
  • 网站建设好做吗乐事薯片软文推广
  • wordpress 年月归档如何优化培训体系
  • 威海高区建设局网站长春做网络优化的公司
  • 安平做网站百度一下首页百度一下知道
  • 苏州建设网站市政中标项目如何做推广引流赚钱
  • 17网站一起做网店怎么下单来宾网站seo
  • 建设商务网站的目的天津seo网站排名优化公司
  • 阿里巴巴网站导航栏怎么做口碑营销策划方案
  • 线上做交互的网站百度app下载
  • 做暖暖欧美网站挖掘爱站网
  • 网站 风格百度推广公司
  • 林州网站建设公司站长工具关键词排名怎么查
  • 想给公司做个网站微信seo是什么意思
  • 网站做管制户外刀具营销推广方案模板
  • 淘宝客网站免费做seo网站关键词优化机构
  • 企业做网站建设的好处seo网站关键词优化
  • 一般网站用什么做的最新新闻国内大事件