gpt4 book ai didi

c++ - 如何创建自定义小部件并在 Qt Designer 中使用它?

转载 作者:行者123 更新时间:2023-11-28 01:19:37 25 4
gpt4 key购买 nike

我非常需要创建我的自定义小部件并在 QtDesigner 中使用它(将 QWidget 提升到我的小部件)。我以前从未做过,也无法用谷歌搜索任何有用的东西。我需要获取的小部件只是带有几个 QLabel 和 QLineEdit 对象的方框。目前我有以下代码:

#include "customwidget01.h"
#include "qlabel.h"
#include "qlineedit.h"
#include "QGridLayout"
customWidget01::customWidget01(QWidget *parent) : QWidget(parent)
{
QString textSheets = "QLabel,QLineEdit {width:60;height:20;max-width:60;max-height:20;;min-width:60;min-height:20;}";
QString widgetSheet = "customWidget01 {width:200;height:200;max-width:200;max-height:200;;min-width:120;min-height:200;}";
this->setStyleSheet(widgetSheet + textSheets);
QLabel *label1= new QLabel(this);
label1->setText("1st arg");
QLabel *label2 = new QLabel(this);
label2->setText("2nd arg");
QLabel *label3= new QLabel(this);
label3->setText("3rd arg");
QLabel *label4= new QLabel(this);
label4->setText("4th arg");
QLineEdit *line1 = new QLineEdit(this);
line1->setPlaceholderText("enter 1st arg");
QLineEdit *line2 = new QLineEdit(this);
line2->setPlaceholderText("enter 2nd arg");
QLineEdit *line3 = new QLineEdit(this);
line3->setPlaceholderText("enter 3rd arg");
QLineEdit *line4 = new QLineEdit(this);
line4->setPlaceholderText("enter 4th arg");
QGridLayout *layout = new QGridLayout();
this->setLayout(layout);
layout->setVerticalSpacing(10);
layout->setHorizontalSpacing(10);
layout->addWidget(label1,0,0);
layout->addWidget(label2,1,0);
layout->addWidget(label3,2,0);
layout->addWidget(label4,3,0);
layout->addWidget(line1,0,1);
layout->addWidget(line2,1,1);
layout->addWidget(line3,2,1);
layout->addWidget(line4,3,1);
this->setVisible(true);
}

我的问题是:

  1. 不能在小部件周围绘制边框
  2. 垂直和水平间距不起作用

一直使用用于 GUI 的 QtDesigner - 不太熟悉纯代码中的 GUI 创建。

最佳答案

让我来帮助你,为了获得漂亮的界面,你需要学习 CSS我会告诉你它是如何工作的这就是你现在拥有的

enter image description here

这意味着你没有正确编写CSS代码

QString textSheets = "QLabel,QLineEdit {width:60;height:20;max-width:60;max-height:20;;min-width:60;min-height:20;}";
QString widgetSheet = "customWidget01 {width:200;height:200;max-width:200;max-height:200;;min-width:120;min-height:200;}";
this->setStyleSheet(widgetSheet + textSheets); // does not work

我会用这些线来交换它

QString textSheets = "QLineEdit{ border-width: 2px; border-style: solid; border-color: red green black rgb(127,255,10); }"
"QLabel { border-width: 2px; border-style: solid; border-color: green black rgb(10,255,180) rgb(180,10,158); }" ;

setStyleSheet(textSheets);

这就是结果

enter image description here

调整大小你只需要这样做

//label1->setMinimumSize(150,50);
label1->setFixedSize(150,50);
//label1->setMaximumSize(150,50);
//label1->setMidLineWidth(150);

这就是结果

enter image description here

关于c++ - 如何创建自定义小部件并在 Qt Designer 中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57102659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com