有个不必现错误,预览初步完成
This commit is contained in:
158
core_form/panelwidget/panelwidget.cpp
Normal file
158
core_form/panelwidget/panelwidget.cpp
Normal file
@@ -0,0 +1,158 @@
|
||||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "panelwidget.h"
|
||||
#include "qscrollarea.h"
|
||||
#include "qframe.h"
|
||||
#include "qboxlayout.h"
|
||||
#include "qdebug.h"
|
||||
|
||||
PanelWidget::PanelWidget(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
scrollArea = new QScrollArea(this);
|
||||
scrollArea->setObjectName("scrollAreaMain");
|
||||
scrollArea->setWidgetResizable(true);
|
||||
|
||||
scrollAreaWidgetContents = new QWidget();
|
||||
scrollAreaWidgetContents->setGeometry(QRect(0, 0, 100, 100));
|
||||
|
||||
verticalLayout = new QVBoxLayout(scrollAreaWidgetContents);
|
||||
verticalLayout->setSpacing(0);
|
||||
verticalLayout->setContentsMargins(0, 0, 0, 0);
|
||||
|
||||
frame = new QFrame(scrollAreaWidgetContents);
|
||||
frame->setObjectName("frameMain");
|
||||
|
||||
gridLayout = new QGridLayout(frame);
|
||||
gridLayout->setSpacing(0);
|
||||
gridLayout->setContentsMargins(0, 0, 0, 0);
|
||||
|
||||
verticalLayout->addWidget(frame);
|
||||
scrollArea->setWidget(scrollAreaWidgetContents);
|
||||
frame->setStyleSheet("QFrame#frameMain{border-width:0px;}");
|
||||
|
||||
margin = 0;
|
||||
space = 0;
|
||||
autoWidth = false;
|
||||
autoHeight = false;
|
||||
}
|
||||
|
||||
void PanelWidget::resizeEvent(QResizeEvent *)
|
||||
{
|
||||
scrollArea->resize(this->size());
|
||||
}
|
||||
|
||||
QSize PanelWidget::sizeHint() const
|
||||
{
|
||||
return QSize(300, 200);
|
||||
}
|
||||
|
||||
QSize PanelWidget::minimumSizeHint() const
|
||||
{
|
||||
return QSize(20, 20);
|
||||
}
|
||||
|
||||
int PanelWidget::getMargin() const
|
||||
{
|
||||
return this->margin;
|
||||
}
|
||||
|
||||
int PanelWidget::getSpace() const
|
||||
{
|
||||
return this->space;
|
||||
}
|
||||
|
||||
bool PanelWidget::getAutoWidth() const
|
||||
{
|
||||
return this->autoWidth;
|
||||
}
|
||||
|
||||
bool PanelWidget::getAutoHeight() const
|
||||
{
|
||||
return this->autoHeight;
|
||||
}
|
||||
|
||||
QList<QWidget *> PanelWidget::getWidgets()
|
||||
{
|
||||
return this->widgets;
|
||||
}
|
||||
|
||||
int PanelWidget::getColumnCount()
|
||||
{
|
||||
return this->columnCount;
|
||||
}
|
||||
|
||||
void PanelWidget::setWidget(QList<QWidget *> widgets, int columnCount)
|
||||
{
|
||||
this->widgets = widgets;
|
||||
this->columnCount = columnCount;
|
||||
|
||||
int row = 0;
|
||||
int column = 0;
|
||||
int index = 0;
|
||||
|
||||
//先把之前的所有移除并不可见
|
||||
foreach (QWidget *widget, widgets) {
|
||||
gridLayout->removeWidget(widget);
|
||||
widget->setVisible(false);
|
||||
}
|
||||
|
||||
//重新添加到布局中并可见
|
||||
foreach (QWidget *widget, widgets) {
|
||||
gridLayout->addWidget(widget, row, column);
|
||||
widget->setVisible(true);
|
||||
|
||||
column++;
|
||||
index++;
|
||||
if (index % columnCount == 0) {
|
||||
row++;
|
||||
column = 0;
|
||||
}
|
||||
}
|
||||
|
||||
row++;
|
||||
|
||||
//设置右边弹簧
|
||||
if (!autoWidth) {
|
||||
QSpacerItem *hSpacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
|
||||
gridLayout->addItem(hSpacer, 0, gridLayout->columnCount());
|
||||
}
|
||||
|
||||
//设置底边弹簧
|
||||
if (!autoHeight) {
|
||||
QSpacerItem *vSpacer = new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding);
|
||||
gridLayout->addItem(vSpacer, row, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void PanelWidget::setMargin(int left, int top, int right, int bottom)
|
||||
{
|
||||
gridLayout->setContentsMargins(left, top, right, bottom);
|
||||
}
|
||||
|
||||
void PanelWidget::setMargin(int margin)
|
||||
{
|
||||
if (this->margin != margin) {
|
||||
setMargin(margin, margin, margin, margin);
|
||||
}
|
||||
}
|
||||
|
||||
void PanelWidget::setSpace(int space)
|
||||
{
|
||||
if (this->space != space) {
|
||||
gridLayout->setSpacing(space);
|
||||
}
|
||||
}
|
||||
|
||||
void PanelWidget::setAutoWidth(bool autoWidth)
|
||||
{
|
||||
if (this->autoWidth != autoWidth) {
|
||||
this->autoWidth = autoWidth;
|
||||
}
|
||||
}
|
||||
|
||||
void PanelWidget::setAutoHeight(bool autoHeight)
|
||||
{
|
||||
if (this->autoHeight != autoHeight) {
|
||||
this->autoHeight = autoHeight;
|
||||
}
|
||||
}
|
||||
74
core_form/panelwidget/panelwidget.h
Normal file
74
core_form/panelwidget/panelwidget.h
Normal file
@@ -0,0 +1,74 @@
|
||||
#ifndef PANELWIDGET_H
|
||||
#define PANELWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QPainterPath>
|
||||
|
||||
class QScrollArea;
|
||||
class QFrame;
|
||||
class QVBoxLayout;
|
||||
class QGridLayout;
|
||||
|
||||
#ifdef quc
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
|
||||
#include <QtDesigner/QDesignerExportWidget>
|
||||
#else
|
||||
#include <QtUiPlugin/QDesignerExportWidget>
|
||||
#endif
|
||||
|
||||
class QDESIGNER_WIDGET_EXPORT PanelWidget : public QWidget
|
||||
#else
|
||||
class PanelWidget : public QWidget
|
||||
#endif
|
||||
|
||||
{
|
||||
Q_OBJECT
|
||||
// Q_PROPERTY(int margin READ getMargin WRITE setMargin)
|
||||
// Q_PROPERTY(int space READ getSpace WRITE setSpace)
|
||||
// Q_PROPERTY(bool autoWidth READ getAutoWidth WRITE setAutoWidth)
|
||||
// Q_PROPERTY(bool autoHeight READ getAutoHeight WRITE setAutoHeight)
|
||||
|
||||
public:
|
||||
explicit PanelWidget(QWidget *parent = 0);
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent *);
|
||||
|
||||
private:
|
||||
QScrollArea *scrollArea; //滚动区域
|
||||
QWidget *scrollAreaWidgetContents; //滚动区域载体
|
||||
QFrame *frame; //放置设备的框架,自动变宽变高
|
||||
QVBoxLayout *verticalLayout; //设备面板总布局
|
||||
QGridLayout *gridLayout; //设备表格布局
|
||||
|
||||
int margin; //边距
|
||||
int space; //设备之间的间隔
|
||||
bool autoWidth; //宽度自动拉伸
|
||||
bool autoHeight; //高度自动拉伸
|
||||
|
||||
QList<QWidget *> widgets; //设备面板对象集合
|
||||
int columnCount; //面板列数
|
||||
|
||||
public:
|
||||
QSize sizeHint() const;
|
||||
QSize minimumSizeHint() const;
|
||||
|
||||
int getMargin() const;
|
||||
int getSpace() const;
|
||||
bool getAutoWidth() const;
|
||||
bool getAutoHeight() const;
|
||||
|
||||
QList<QWidget *> getWidgets();
|
||||
int getColumnCount();
|
||||
|
||||
public Q_SLOTS:
|
||||
void setWidget(QList<QWidget *> widgets, int columnCount);
|
||||
void setMargin(int left, int top, int right, int bottom);
|
||||
void setMargin(int margin);
|
||||
void setSpace(int space);
|
||||
void setAutoWidth(bool autoWidth);
|
||||
void setAutoHeight(bool autoHeight);
|
||||
|
||||
};
|
||||
|
||||
#endif // PANELWIDGET_H
|
||||
5
core_form/panelwidget/panelwidget.pri
Normal file
5
core_form/panelwidget/panelwidget.pri
Normal file
@@ -0,0 +1,5 @@
|
||||
HEADERS += \
|
||||
$$PWD/panelwidget.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/panelwidget.cpp
|
||||
Reference in New Issue
Block a user