软件系统配置UI(QT)
作者:互联网
#ifndef XCONFIGDLG_H #define XCONFIGDLG_H #include <QFrame> #include <QDialog> #include <QScrollArea> #include <QScrollBar> #include <QListWidget> #include <QVBoxLayout> #include <QGridLayout> #include <QList> #include <QLabel> #include <QCheckBox> class XConfigDlg : public QDialog { Q_OBJECT public: explicit XConfigDlg(QWidget *parent = nullptr); Q_SIGNALS: public Q_SLOTS: void slotOnItemClicked(QListWidgetItem *item); void onSlotScrollBarValueChanged(int value); private: QFrame *m_layoutWidget = nullptr; //右侧Widget QStringList m_itemNameList; //左侧QListWidget项目名称 QVector<QWidget *> m_widgetList; //右侧项目Widget集合 QVector<int> m_widgetBottomYPos; //右侧项目Widget底部Y坐标 QListWidget *m_leftWidget = nullptr; //左侧QListWidget QScrollArea *m_rightWidget = nullptr; //右侧滚动区域 QVBoxLayout *m_pContentVBoxLayout = nullptr; //右侧m_layoutWidget的布局管理器 int m_nWidgets = 30; void initCtrl(); public: }; #endif // XCONFIGDLG_H
#include "xconfigdlg.h" #include <QDebug> #include <QRandomGenerator> XConfigDlg::XConfigDlg(QWidget *parent) : QDialog(parent) { setFixedSize(700, 480); initCtrl(); } void XConfigDlg::initCtrl() { int leftWidth = 130; int rightWidth = width() - leftWidth; m_leftWidget = new QListWidget(this); m_leftWidget->setStyleSheet("QFrame{background-color:rgb(180,180,180)}"); m_rightWidget = new QScrollArea(this); m_leftWidget->setGeometry(0, 0, 130, height()); m_rightWidget->setGeometry(leftWidth, 0, rightWidth, height()); for(int i = 0; i < m_nWidgets; i++){ m_itemNameList << QString("item %1").arg(i+1); } m_layoutWidget = new QFrame(this); //为标签生成随机背景颜色 QStringList styleSheets; for(int i = 0; i < m_nWidgets; i++){ styleSheets << QString("QFrame{background-color:rgb(%1,%2,%3)}") .arg(QRandomGenerator::global()->bounded(0,256)) .arg(QRandomGenerator::global()->bounded(0,256)) .arg(QRandomGenerator::global()->bounded(0,256)); } m_pContentVBoxLayout = new QVBoxLayout(m_layoutWidget); m_pContentVBoxLayout->setContentsMargins(0,0,0,0); m_pContentVBoxLayout->setSpacing(0); int currentY = 0; for(int i = 0; i < m_itemNameList.size(); i++){ QLabel *frame = new QLabel(m_itemNameList[i], m_layoutWidget); quint32 randHeight = QRandomGenerator::global()->bounded(30,300); frame->setFixedSize(rightWidth, randHeight); frame->setStyleSheet(styleSheets.at(i)); m_widgetList.append(frame); m_pContentVBoxLayout->addWidget(frame); currentY += frame->height(); m_widgetBottomYPos.push_back(currentY); } m_leftWidget->addItems(m_itemNameList); connect(m_leftWidget, &QListWidget::itemClicked, this, &XConfigDlg::slotOnItemClicked); m_leftWidget->setCurrentRow(0); m_rightWidget->setWidgetResizable(true); m_rightWidget->setWidget(m_layoutWidget); connect(m_rightWidget->verticalScrollBar(), &QScrollBar::valueChanged, this, &XConfigDlg::onSlotScrollBarValueChanged); } void XConfigDlg::slotOnItemClicked(QListWidgetItem *item) { int rowIndex = m_leftWidget->row(item); int bottomYPos = 0; if(rowIndex != 0){ bottomYPos = m_widgetBottomYPos[rowIndex-1]; } m_rightWidget->verticalScrollBar()->setValue(bottomYPos); } void XConfigDlg::onSlotScrollBarValueChanged(int value) { qDebug() << __FUNCTION__; int scrollYPos = value; qDebug() << "scrollYPos:" << scrollYPos; for(int i = 0; i < m_widgetBottomYPos.size(); i++){ if(m_widgetBottomYPos[i] > scrollYPos){ m_leftWidget->setCurrentRow(i); break; } } }
标签:rightWidget,QT,int,void,XConfigDlg,UI,leftWidget,include,软件系统 来源: https://www.cnblogs.com/endenvor/p/15785393.html