转载 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
#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_Hmylistwidget.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_Hmywinbox.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
#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
//#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(); }
#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; }mylistwidget.cpp
#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; }
#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() { }
#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); }在编译的时候,一直出现链接 问题,把工程清理了,重新编译都不行,后来 把项目的生成的那一套 build-untitled-Desktop_Qt_5_3_MSVC2010_OpenGL_32bit_qt_53_w-Debug 整个文件夹给删除了,就可以了。看来QT中只要代码正确,保证头文件中的声明和实现中有定义,代码正确,就可以把生成的编译的东西删除,再编译,就可以了。原先一直在编译,然后一直不成功,刚开始一直怀疑是代码的问题,弄了好半天,最后原来是这个问题~~~~~~~~~~~~~~