1、先写固定的label宽度;

feature/struct-menu-20241023
simonyan 2 weeks ago
parent 75fd950ebf
commit a8ea239566

@ -1,4 +1,4 @@
#include "CUIComboBox.h" #include "CUIComboBox.h"
#include<CUIConfig.h> #include<CUIConfig.h>
#include<QHBoxLayout> #include<QHBoxLayout>
@ -12,9 +12,9 @@
* @param conf * @param conf
* @param parent * @param parent
*/ */
CUIComboBox::CUIComboBox(CUIConfig* conf, QVector<CUI*> &subCUI, QWidget *parent) : QWidget(parent) CUIComboBox::CUIComboBox(CUIConfig* conf, QVector<CUI*> &subCUI, QWidget *parent) : CUIComponentBaseWidget(parent)
{ {
this->conf = conf; this->m_conf = conf;
initUI(subCUI); initUI(subCUI);
} }
@ -24,7 +24,7 @@ CUIComboBox::CUIComboBox(CUIConfig* conf, QVector<CUI*> &subCUI, QWidget *parent
*/ */
qint32 CUIComboBox::getLabelWidth() qint32 CUIComboBox::getLabelWidth()
{ {
return label->minimumSizeHint().width(); return m_label->minimumSizeHint().width();
} }
/** /**
@ -33,7 +33,7 @@ qint32 CUIComboBox::getLabelWidth()
*/ */
void CUIComboBox::setLabelWidth(qint32 width) void CUIComboBox::setLabelWidth(qint32 width)
{ {
label->setMinimumWidth(width); m_label->setMinimumWidth(width);
} }
@ -43,18 +43,17 @@ void CUIComboBox::setLabelWidth(qint32 width)
*/ */
void CUIComboBox::initUI(QVector<CUI*> &subCUI) void CUIComboBox::initUI(QVector<CUI*> &subCUI)
{ {
label = new QLabel(conf->getPropertyValue("name")); m_label = new QLabel(m_conf->getPropertyValue("name"));
comboBox = new QComboBox; m_label->setFixedWidth(m_labelLength);
layout = new QHBoxLayout; m_comboBox = new QComboBox;
this->setLayout(layout); m_layout = new QHBoxLayout;
layout->addWidget(label); this->setLayout(m_layout);
layout->addWidget(comboBox); m_layout->addWidget(m_label);
layout->setStretchFactor(comboBox, 1); m_layout->addWidget(m_comboBox);
((QHBoxLayout*)m_layout)->setStretchFactor(m_comboBox, 1);
iniComoboBox(); iniComoboBox();
connect(m_comboBox, QOverload<int>::of(&QComboBox::activated), [ = ](int idx) {
connect(comboBox, QOverload<int>::of(&QComboBox::activated), [ = ](int idx) { this->m_conf->setValue(idx);
this->conf->setValue(idx);
}); });
} }
@ -63,26 +62,24 @@ void CUIComboBox::initUI(QVector<CUI*> &subCUI)
*/ */
void CUIComboBox::iniComoboBox() void CUIComboBox::iniComoboBox()
{ {
for(auto cui : conf->getSub()) { for(auto cui : m_conf->getSub()) {
QString type = cui->getPropertyValue("type"); QString type = cui->getPropertyValue("type");
if(type != "Item") { if(type != "Item") {
return; return;
} }
CUI* temp = new CUI(nullptr, cui); CUI* temp = new CUI(nullptr, cui);
Items.push_back(temp); m_items.push_back(temp);
if(temp->getProperty("data_type") == "int") { if(temp->getProperty("data_type") == "int") {
comboBox->addItem(temp->getProperty("name"), temp->getProperty("data").toInt()); m_comboBox->addItem(temp->getProperty("name"), temp->getProperty("data").toInt());
} else if(temp->getProperty("data_type") == "double") { } else if(temp->getProperty("data_type") == "double") {
comboBox->addItem(temp->getProperty("name"), temp->getProperty("data").toDouble()); m_comboBox->addItem(temp->getProperty("name"), temp->getProperty("data").toDouble());
} else { } else {
comboBox->addItem(temp->getProperty("name"), temp->getProperty("data")); m_comboBox->addItem(temp->getProperty("name"), temp->getProperty("data"));
} }
} }
QVariant vOrigin = conf->getPropertyOriginValue("value_origin"); QVariant vOrigin = m_conf->getPropertyOriginValue("value_origin");
int *ptr = qvariant_cast<int *>(vOrigin); int *ptr = qvariant_cast<int *>(vOrigin);
if(ptr!=nullptr) if(ptr != nullptr) {
{ m_comboBox->setCurrentIndex(*ptr);
comboBox->setCurrentIndex(*ptr);
} }
} }

@ -1,7 +1,9 @@
#ifndef CUICOMBOBOX_H #ifndef CUICOMBOBOX_H
#define CUICOMBOBOX_H #define CUICOMBOBOX_H
#include <QWidget> #include <QWidget>
#include "CUIComponentBaseWidget.h"
#include "CUIComponentBase.h"
#include <QComboBox> #include <QComboBox>
class CUIConfig; class CUIConfig;
@ -9,7 +11,7 @@ class CUI;
class QLabel; class QLabel;
class QHBoxLayout; class QHBoxLayout;
class CUIComboBox : public QWidget class CUIComboBox : public CUIComponentBaseWidget, public CUIComponentBase
{ {
Q_OBJECT Q_OBJECT
public: public:
@ -17,13 +19,11 @@ public:
qint32 getLabelWidth(); qint32 getLabelWidth();
void setLabelWidth(qint32 width); void setLabelWidth(qint32 width);
private: private:
void initUI(QVector<CUI*> &subCUI); virtual void initUI(QVector<CUI*> &subCUI);
void iniComoboBox(); void iniComoboBox();
QComboBox* comboBox; QComboBox* m_comboBox;
QLabel* label; QLabel* m_label;
CUIConfig* conf; QVector<CUI*> m_items;
QVector<CUI*> Items;
QHBoxLayout* layout;
signals: signals:
}; };

@ -1,4 +1,4 @@
#ifndef CUICOMPONENTBASEWIDGET_H #ifndef CUICOMPONENTBASEWIDGET_H
#define CUICOMPONENTBASEWIDGET_H #define CUICOMPONENTBASEWIDGET_H
#include <QWidget> #include <QWidget>
@ -14,6 +14,7 @@ protected:
virtual void initUI(); virtual void initUI();
protected: protected:
QLayout* m_layout = nullptr; QLayout* m_layout = nullptr;
int m_labelLength = 100;
signals: signals:
}; };

@ -1,4 +1,4 @@
#include "CUILineEdit.h" #include "CUILineEdit.h"
#include <QHBoxLayout> #include <QHBoxLayout>
#include <CUIConfig.h> #include <CUIConfig.h>
@ -46,6 +46,7 @@ void CUILineEdit::initUI(QVector<CUI*> &subCUI)
{ {
m_layout = new QHBoxLayout(); m_layout = new QHBoxLayout();
m_label = new QLabel(m_conf->getPropertyValue("name")); m_label = new QLabel(m_conf->getPropertyValue("name"));
m_label->setFixedWidth(m_labelLength);
m_lineEdit = new QLineEdit(this->getValueString()); m_lineEdit = new QLineEdit(this->getValueString());
m_layout->addWidget(m_label); m_layout->addWidget(m_label);
m_layout->addWidget(m_lineEdit); m_layout->addWidget(m_lineEdit);
@ -82,7 +83,7 @@ void CUILineEdit::setInputTips()
msg += m_inclusiveMax ? "]" : ")"; msg += m_inclusiveMax ? "]" : ")";
} }
if(m_required) { if(m_required) {
msg += "\t(必填)"; msg += "\t" + tr("(required)");
} }
m_lineEdit->setPlaceholderText(msg); m_lineEdit->setPlaceholderText(msg);
} }

@ -1,4 +1,4 @@
#include "CUIMultiTableWidget.h" #include "CUIMultiTableWidget.h"
#include <QTableWidget> #include <QTableWidget>
#include <QTableWidgetItem> #include <QTableWidgetItem>
#include <QHeaderView> #include <QHeaderView>
@ -16,8 +16,8 @@ CUIMultiTableWidget::CUIMultiTableWidget(CUIConfig* conf, QVector<CUI*> &subCUI,
void CUIMultiTableWidget::initUI() void CUIMultiTableWidget::initUI()
{ {
QStringList headers; QStringList headers;
headers<<tr("index")<<tr("name")<<tr("view"); headers << tr("index") << tr("name") << tr("alias") << tr("view");
QList<QList<QString>> items; QList<QList<QString >> items;
QList<QString> item1 = {tr("quaility"), "quaility"}; QList<QString> item1 = {tr("quaility"), "quaility"};
QList<QString> item2 = {tr("x"), "x"}; QList<QString> item2 = {tr("x"), "x"};
QList<QString> item3 = {tr("y"), "y"}; QList<QString> item3 = {tr("y"), "y"};
@ -28,7 +28,7 @@ void CUIMultiTableWidget::initUI()
items.append(item4); items.append(item4);
// 根据配置来初始化tableWidget // 根据配置来初始化tableWidget
m_tableWidget = new QTableWidget(this); m_tableWidget = new QTableWidget(this);
m_tableWidget->setColumnCount(items.count() + 2); m_tableWidget->setColumnCount(items[0].count() + 2);
m_tableWidget->setRowCount(items.count()); m_tableWidget->setRowCount(items.count());
m_tableWidget->setHorizontalHeaderLabels(headers); m_tableWidget->setHorizontalHeaderLabels(headers);
// 设置水平表头的背景颜色 // 设置水平表头的背景颜色
@ -38,29 +38,27 @@ void CUIMultiTableWidget::initUI()
// 隐藏垂直表头 // 隐藏垂直表头
QHeaderView *verticalHeader = m_tableWidget->verticalHeader(); QHeaderView *verticalHeader = m_tableWidget->verticalHeader();
verticalHeader->setVisible(false); verticalHeader->setVisible(false);
for (int i = 0; i < items.count(); i++) {
for (int i=0;i<items.count();i++) {
QList<QString> item = items[i]; QList<QString> item = items[i];
// 序号 // 序号
QTableWidgetItem* indexItem = new QTableWidgetItem(QString("%1").arg(i+1)); QTableWidgetItem* indexItem = new QTableWidgetItem(QString("%1").arg(i + 1));
indexItem->setFlags(indexItem->flags() &~Qt::ItemIsSelectable); indexItem->setFlags(indexItem->flags() & ~Qt::ItemIsSelectable);
indexItem->setTextAlignment(Qt::AlignCenter); // 设置水平居中 indexItem->setTextAlignment(Qt::AlignCenter); // 设置水平居中
m_tableWidget->setItem(i,0, indexItem); m_tableWidget->setItem(i, 0, indexItem);
// 内容 // 内容
for (int j=0;j<item.count();j++) { for (int j = 0; j < item.count(); j++) {
QTableWidgetItem* nameItem = new QTableWidgetItem(item[j]); QTableWidgetItem* nameItem = new QTableWidgetItem(item[j]);
nameItem->setFlags(indexItem->flags() &~Qt::ItemIsSelectable); nameItem->setFlags(indexItem->flags() & ~Qt::ItemIsSelectable);
nameItem->setTextAlignment(Qt::AlignCenter); // 设置水平居中 nameItem->setTextAlignment(Qt::AlignCenter); // 设置水平居中
m_tableWidget->setItem(i,j+1, nameItem); m_tableWidget->setItem(i, j + 1, nameItem);
} }
// 复选框 // 复选框
QTableWidgetItem *checkBoxItem = new QTableWidgetItem(); QTableWidgetItem *checkBoxItem = new QTableWidgetItem();
m_tableWidget->setItem(i,item.count() + 1, checkBoxItem); m_tableWidget->setItem(i, item.count() + 1, checkBoxItem);
QCheckBox* checkBox = new QCheckBox(); QCheckBox* checkBox = new QCheckBox();
checkBox->setStyleSheet("margin-left:45%; margin-right:45%;");// 设置水平居中 checkBox->setStyleSheet("margin-left:45%; margin-right:45%;");// 设置水平居中
m_tableWidget->setCellWidget(i, item.count() + 1, checkBox); m_tableWidget->setCellWidget(i, item.count() + 1, checkBox);
} }
m_layout = new QVBoxLayout; m_layout = new QVBoxLayout;
m_layout->addWidget(m_tableWidget); m_layout->addWidget(m_tableWidget);
this->setLayout(m_layout); this->setLayout(m_layout);

Loading…
Cancel
Save