转载 http://zhouzhenren163.blog.163.com/blog/static/6549928120140605729334/
效果如下图所示:
代码如下:
untitled.pro:
- #-------------------------------------------------
- #
- # Project created by QtCreator 2015-12-03T15:32:15
- #
- #-------------------------------------------------
- QT += core gui
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
- TARGET = untitled
- TEMPLATE = app
- SOURCES += main.cpp\
- #mainwindow.cpp \
- test.cpp \
- mylistwidget.cpp \
- mylistview.cpp \
- mywinbox.cpp
- HEADERS +=Enum.h \
- test.h \
- mylistwidget.h \
- mylistview.h \
- mywinbox.h\
- #mainwindow.h \
- FORMS += mainwindow.ui
- #ifndef ENUM_H
- #define ENUM_H
- enum MyEnum
- {
- GRAP = 2, //控件四周间隙,可更改;
- WIDTH = 180+2*GRAP, //+2*GRAP不能更改,只能改前面的数字,如将400改为300或500,下同;
- HEIGHT = 70+2*GRAP,
- COUNT = 10
- };
- #endif // ENUM_H
mylistview.h:
- #ifndef MYLISTVIEW_H
- #define MYLISTVIEW_H
- #include <QListView>
- #include <QList>
- #include "MyWinBox.h"
- class MyListView : public QListView
- {
- Q_OBJECT
- public:
- MyListView(QWidget *parent);
- ~MyListView();
- public slots:
- void ReSizeSlot(int row,int column);
- public:
- QList<MyWinBox*> GetWinBoxList();
- private:
- QList<MyWinBox*> m_WinBoxList;
- };
- #endif // MYLISTVIEW_H
- #ifndef MYLISTWIDGET_H
- #define MYLISTWIDGET_H
- #include <QWidget>
- #include <QListView>
- #include "MyListView.h"
- class MyListWidget : public QWidget
- {
- Q_OBJECT
- public:
- MyListWidget(QWidget *parent);
- ~MyListWidget();
- public:
- MyListView* GetList();
- private:
- MyListView* m_ListView;
- };
- #endif // MYLISTWIDGET_H
- #ifndef MYWINBOX_H
- #define MYWINBOX_H
- #include <QWidget>
- #include <QLineEdit>
- class MyWinBox : public QWidget
- {
- Q_OBJECT
- public:
- MyWinBox(QWidget *parent);
- ~MyWinBox();
- public:
- QLineEdit* m_NameEdit;
- };
- #endif // MYWINBOX_H
test.h
- #ifndef TEST_H
- #define TEST_H
- #include <QWidget>
- #include "mylistwidget.h"
- #include <QScrollBar>
- class Test : public QWidget
- {
- Q_OBJECT
- public:
- Test(QWidget *parent = 0);
- ~Test();
- //overwrite:
- protected:
- void resizeEvent(QResizeEvent * event);
- signals:
- void SendReSizeSignal(int,int);
- private slots:
- void DisplayListView(int);
- private:
- MyListWidget* m_ListWidget;
- QScrollBar* m_VScrollBar ;
- };
- #endif // TEST_H
main.cpp:
- //#include "mainwindow.h"
- #include <QtWidgets/QApplication>
- #include "test.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- // MainWindow w;
- // w.show();
- Test w;
- w.show();
- return a.exec();
- }
mylistview.cpp
- #include "mylistview.h"
- #include "Enum.h"
- MyListView::MyListView(QWidget *parent) : QListView(parent)
- {
- for(int i=0;i<COUNT;i++)
- {
- MyWinBox* box = new MyWinBox(this);
- box->m_NameEdit->setText(QString("%0").arg(i+1));
- box->move(0,i*(HEIGHT)+(i+1)*GRAP);
- m_WinBoxList.append(box);
- }
- }
- MyListView::~MyListView()
- {
- }
- void MyListView::ReSizeSlot(int row,int column)
- {
- int x=0,y=0;
- int n = 0;
- for(int i=0;i<row;i++)
- {
- y = i * (HEIGHT+GRAP) + 2*GRAP;
- for(int j=0;j<column;j++)
- {
- x = j * (WIDTH+GRAP) + 2*GRAP;
- if(n>=COUNT)
- break;
- m_WinBoxList[n++]->move(x,y);
- }
- }
- }
- QList<MyWinBox*> MyListView::GetWinBoxList()
- {
- return m_WinBoxList;
- }
- #include "MyListWidget.h"
- #include <QLayout>
- MyListWidget::MyListWidget(QWidget *parent) : QWidget(parent)
- {
- m_ListView = new MyListView(this);
- QGridLayout* layout = new QGridLayout(this);
- layout->addWidget(m_ListView);
- layout->setMargin(0);
- layout->setSpacing(0);
- setLayout(layout);
- }
- MyListWidget::~MyListWidget()
- {
- }
- MyListView* MyListWidget::GetList()
- {
- return m_ListView;
- }
mywinbox.cpp
- #include "MyWinBox.h"
- #include <QLabel>
- #include <QCheckBox>
- #include <QLayout>
- #include <QGroupBox>
- #include "Enum.h"
- MyWinBox::MyWinBox(QWidget *parent) : QWidget(parent)
- {
- QLabel* name = new QLabel("name:",this);
- m_NameEdit = new QLineEdit(this);
- m_NameEdit->setFixedWidth(100);
- QLabel* sex = new QLabel("sex:",this);
- QCheckBox* check = new QCheckBox(this);
- QGroupBox* groupBox = new QGroupBox(this);
- QGridLayout* layout = new QGridLayout(this);
- layout->addWidget(name,0,0);
- layout->addWidget(m_NameEdit,0,1);
- layout->addWidget(sex,1,0);
- layout->addWidget(check,1,1);
- groupBox->setLayout(layout);
- groupBox->setAlignment(Qt::AlignCenter);
- this->setFixedSize(WIDTH,2*(HEIGHT+GRAP));
- QString groupBoxStyle = "QGroupBox{background: qradialgradient(cx:0, cy:0, radius: 1,fx:0.5, fy:0.5, stop:0 white, stop:1 rgba(0,0,0, 50%));border-radius: 3px}";
- groupBox->setStyleSheet(groupBoxStyle);
- }
- MyWinBox::~MyWinBox()
- {
- }
test.cpp
- #include "test.h"
- #include <QLayout>
- #include "Enum.h"
- /*
- Test::Test(QWidget *parent) : QWidget(parent)
- {
- m_ListWidget = new MyListWidget(this);
- m_VScrollBar = new QScrollBar(Qt::Vertical,this);
- QHBoxLayout* layout = new QHBoxLayout(this);
- layout->addWidget(m_ListWidget);
- layout->addWidget(m_VScrollBar);
- layout->setMargin(0);
- layout->setSpacing(0);
- setLayout(layout);
- connect(m_VScrollBar,SIGNAL(valueChanged(int)),this,SLOT(DisplayListView(int)));
- connect(this,SIGNAL(SendReSizeSignal(int,int)),m_ListWidget->GetList(),SLOT(ReSizeSlot(int,int)));
- }
- Test::~Test()
- {
- }
- void Test::resizeEvent(QResizeEvent * event)
- {
- int width = this->size().width()-m_VScrollBar->width();
- int column = width/WIDTH;
- column = width/WIDTH==0 ? 1 : column;
- int row = COUNT/column;
- row = COUNT%column==0 ? row : (row+1) ;
- if(height()>=row*HEIGHT)
- {
- m_VScrollBar->setMinimum(0);
- m_VScrollBar->setMaximum(0);
- m_VScrollBar->setHidden(true);
- }
- else
- {
- m_VScrollBar->setHidden(false);
- int itemHeight = HEIGHT+GRAP;
- m_VScrollBar->setMinimum(0);
- int max = row*itemHeight-height();
- int pageStep = height()-max;
- m_VScrollBar->setMaximum(max);
- m_VScrollBar->setPageStep(pageStep);
- m_VScrollBar->setSingleStep(10);
- }
- int listViewHeight = row*(HEIGHT+GRAP);
- listViewHeight = listViewHeight > this->size().height() ? listViewHeight : this->size().height();
- m_ListWidget->GetList()->resize(this->size().width(),listViewHeight);
- emit SendReSizeSignal(row,column);
- QWidget::resizeEvent(event);
- }
- void Test::DisplayListView(int len)
- {
- m_ListWidget->GetList()->move(0,-len);
- }
- */
- Test::Test(QWidget *parent) : QWidget(parent)
- {
- m_ListWidget = new MyListWidget(this);
- m_VScrollBar = new QScrollBar(Qt::Vertical,this);
- QHBoxLayout* layout = new QHBoxLayout(this);
- layout->addWidget(m_ListWidget);
- layout->addWidget(m_VScrollBar);
- layout->setMargin(0);
- layout->setSpacing(0);
- setLayout(layout);
- connect(m_VScrollBar,SIGNAL(valueChanged(int)),this,SLOT(DisplayListView(int)));
- connect(this,SIGNAL(SendReSizeSignal(int,int)),m_ListWidget->GetList(),SLOT(ReSizeSlot(int,int)));
- }
- Test::~Test()
- {
- }
- void Test::resizeEvent(QResizeEvent * event)
- {
- int width = this->size().width()-m_VScrollBar->width();
- int column = width/WIDTH;
- column = width/WIDTH==0 ? 1 : column;
- int row = COUNT/column;
- row = COUNT%column==0 ? row : (row+1) ;
- if(height()>=row*HEIGHT)
- {
- m_VScrollBar->setMinimum(0);
- m_VScrollBar->setMaximum(0);
- m_VScrollBar->setHidden(true);
- }
- else
- {
- m_VScrollBar->setHidden(false);
- int itemHeight = HEIGHT+GRAP;
- m_VScrollBar->setMinimum(0);
- int max = row*itemHeight-height();
- int pageStep = height()-max;
- m_VScrollBar->setMaximum(max);
- m_VScrollBar->setPageStep(pageStep);
- m_VScrollBar->setSingleStep(10);
- }
- int listViewHeight = row*(HEIGHT+GRAP);
- listViewHeight = listViewHeight > this->size().height() ? listViewHeight : this->size().height();
- m_ListWidget->GetList()->resize(this->size().width(),listViewHeight);
- emit SendReSizeSignal(row,column);
- QWidget::resizeEvent(event);
- }
- void Test::DisplayListView(int len)
- {
- m_ListWidget->GetList()->move(0,-len);
- }