QT中QListView中放置自定义控件并添加滚动条

来源:互联网 发布:jquery.jsoncookie.js 编辑:程序博客网 时间:2024/06/05 02:47

转载 http://zhouzhenren163.blog.163.com/blog/static/6549928120140605729334/ 

效果如下图所示:


代码如下:

untitled.pro:

[cpp] view plain copy
  1. #-------------------------------------------------  
  2. #  
  3. # Project created by QtCreator 2015-12-03T15:32:15  
  4. #  
  5. #-------------------------------------------------  
  6.   
  7. QT       += core gui  
  8.   
  9. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets  
  10.   
  11. TARGET = untitled  
  12. TEMPLATE = app  
  13.   
  14.   
  15. SOURCES += main.cpp\  
  16.         #mainwindow.cpp \  
  17.     test.cpp \  
  18.     mylistwidget.cpp \  
  19.     mylistview.cpp \  
  20.     mywinbox.cpp  
  21.   
  22. HEADERS  +=Enum.h \  
  23.     test.h \  
  24.     mylistwidget.h \  
  25.     mylistview.h \  
  26.     mywinbox.h\  
  27. #mainwindow.h \  
  28.   
  29. FORMS    += mainwindow.ui  


Enum.h:

[cpp] view plain copy
  1. #ifndef ENUM_H  
  2. #define ENUM_H  
  3. enum MyEnum  
  4. {  
  5.     GRAP = 2,               //控件四周间隙,可更改;  
  6.     WIDTH = 180+2*GRAP,     //+2*GRAP不能更改,只能改前面的数字,如将400改为300或500,下同;  
  7.     HEIGHT = 70+2*GRAP,  
  8.     COUNT = 10  
  9. };  
  10. #endif // ENUM_H  

mylistview.h:

[cpp] view plain copy
  1. #ifndef MYLISTVIEW_H  
  2. #define MYLISTVIEW_H  
  3.   
  4. #include <QListView>  
  5. #include <QList>  
  6.   
  7. #include "MyWinBox.h"  
  8.   
  9.   
  10. class MyListView : public QListView  
  11. {  
  12.     Q_OBJECT  
  13.   
  14. public:  
  15.     MyListView(QWidget *parent);  
  16.     ~MyListView();  
  17.   
  18. public slots:  
  19.     void ReSizeSlot(int row,int column);  
  20.   
  21. public:  
  22.     QList<MyWinBox*>  GetWinBoxList();  
  23.   
  24. private:  
  25.     QList<MyWinBox*>  m_WinBoxList;  
  26.   
  27. };  
  28.   
  29. #endif // MYLISTVIEW_H  
mylistwidget.h:

[cpp] view plain copy
  1. #ifndef MYLISTWIDGET_H  
  2. #define MYLISTWIDGET_H  
  3.   
  4. #include <QWidget>  
  5. #include <QListView>  
  6. #include "MyListView.h"  
  7.   
  8. class MyListWidget : public QWidget  
  9. {  
  10.     Q_OBJECT  
  11. public:  
  12.     MyListWidget(QWidget *parent);  
  13.     ~MyListWidget();  
  14. public:  
  15.     MyListView*  GetList();  
  16. private:  
  17.     MyListView*  m_ListView;  
  18. };  
  19.   
  20. #endif // MYLISTWIDGET_H  
mywinbox.h:

[cpp] view plain copy
  1. #ifndef MYWINBOX_H  
  2. #define MYWINBOX_H  
  3.   
  4. #include <QWidget>  
  5. #include <QLineEdit>  
  6.   
  7. class MyWinBox : public QWidget  
  8. {  
  9.     Q_OBJECT  
  10.   
  11. public:  
  12.     MyWinBox(QWidget *parent);  
  13.     ~MyWinBox();  
  14.   
  15. public:  
  16.     QLineEdit*  m_NameEdit;  
  17. };  
  18.   
  19. #endif // MYWINBOX_H  

test.h

[cpp] view plain copy
  1. #ifndef TEST_H  
  2. #define TEST_H  
  3.   
  4. #include <QWidget>  
  5. #include "mylistwidget.h"  
  6. #include <QScrollBar>  
  7.   
  8. class Test : public QWidget  
  9. {  
  10.     Q_OBJECT  
  11.   
  12. public:  
  13.     Test(QWidget *parent = 0);  
  14.     ~Test();  
  15.     //overwrite:  
  16. protected:  
  17.     void  resizeEvent(QResizeEvent * event);  
  18.   
  19. signals:  
  20.     void SendReSizeSignal(int,int);  
  21. private slots:  
  22.     void DisplayListView(int);  
  23.   
  24. private:  
  25.     MyListWidget*   m_ListWidget;  
  26.     QScrollBar*     m_VScrollBar ;  
  27.   
  28. };  
  29. #endif // TEST_H  

main.cpp:

[cpp] view plain copy
  1. //#include "mainwindow.h"  
  2. #include <QtWidgets/QApplication>  
  3. #include "test.h"  
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     QApplication a(argc, argv);  
  8. //    MainWindow w;  
  9. //    w.show();  
  10.     Test w;  
  11.     w.show();  
  12.   
  13.     return a.exec();  
  14. }  

mylistview.cpp

[cpp] view plain copy
  1. #include "mylistview.h"  
  2. #include "Enum.h"  
  3.   
  4. MyListView::MyListView(QWidget *parent) : QListView(parent)  
  5. {  
  6.     for(int i=0;i<COUNT;i++)  
  7.     {  
  8.         MyWinBox* box = new MyWinBox(this);  
  9.         box->m_NameEdit->setText(QString("%0").arg(i+1));  
  10.         box->move(0,i*(HEIGHT)+(i+1)*GRAP);  
  11.         m_WinBoxList.append(box);  
  12.     }  
  13. }  
  14.   
  15. MyListView::~MyListView()  
  16. {  
  17.   
  18. }  
  19. void MyListView::ReSizeSlot(int row,int column)  
  20. {  
  21.     int x=0,y=0;  
  22.     int n = 0;  
  23.     for(int i=0;i<row;i++)  
  24.     {  
  25.         y = i * (HEIGHT+GRAP) + 2*GRAP;  
  26.         for(int j=0;j<column;j++)  
  27.         {  
  28.             x = j * (WIDTH+GRAP) + 2*GRAP;  
  29.             if(n>=COUNT)  
  30.                 break;  
  31.             m_WinBoxList[n++]->move(x,y);  
  32.         }  
  33.     }  
  34.   
  35. }  
  36.   
  37. QList<MyWinBox*>  MyListView::GetWinBoxList()  
  38. {  
  39.     return m_WinBoxList;  
  40. }  
mylistwidget.cpp

[cpp] view plain copy
  1. #include "MyListWidget.h"  
  2. #include <QLayout>  
  3.   
  4.   
  5. MyListWidget::MyListWidget(QWidget *parent) : QWidget(parent)  
  6. {  
  7.     m_ListView = new MyListView(this);  
  8.   
  9.     QGridLayout* layout = new QGridLayout(this);  
  10.     layout->addWidget(m_ListView);  
  11.     layout->setMargin(0);  
  12.     layout->setSpacing(0);  
  13.     setLayout(layout);  
  14. }  
  15.   
  16. MyListWidget::~MyListWidget()  
  17. {  
  18.   
  19. }  
  20.   
  21. MyListView* MyListWidget::GetList()  
  22. {  
  23.     return m_ListView;  
  24. }  

mywinbox.cpp

[cpp] view plain copy
  1. #include "MyWinBox.h"  
  2. #include <QLabel>  
  3. #include <QCheckBox>  
  4. #include <QLayout>  
  5. #include <QGroupBox>  
  6. #include "Enum.h"  
  7.   
  8.   
  9. MyWinBox::MyWinBox(QWidget *parent) : QWidget(parent)  
  10. {  
  11.     QLabel*  name = new QLabel("name:",this);  
  12.     m_NameEdit = new QLineEdit(this);  
  13.     m_NameEdit->setFixedWidth(100);  
  14.     QLabel* sex = new QLabel("sex:",this);  
  15.     QCheckBox* check = new QCheckBox(this);  
  16.   
  17.     QGroupBox* groupBox = new QGroupBox(this);  
  18.   
  19.     QGridLayout* layout = new QGridLayout(this);  
  20.     layout->addWidget(name,0,0);  
  21.     layout->addWidget(m_NameEdit,0,1);  
  22.     layout->addWidget(sex,1,0);  
  23.     layout->addWidget(check,1,1);  
  24.     groupBox->setLayout(layout);  
  25.     groupBox->setAlignment(Qt::AlignCenter);  
  26.   
  27.     this->setFixedSize(WIDTH,2*(HEIGHT+GRAP));  
  28.   
  29.     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}";  
  30.     groupBox->setStyleSheet(groupBoxStyle);  
  31. }  
  32.   
  33. MyWinBox::~MyWinBox()  
  34. {  
  35.   
  36. }  

test.cpp

[cpp] view plain copy
  1. #include "test.h"  
  2. #include <QLayout>  
  3. #include "Enum.h"  
  4.   
  5. /* 
  6. Test::Test(QWidget *parent)  : QWidget(parent) 
  7. { 
  8.     m_ListWidget = new MyListWidget(this); 
  9.     m_VScrollBar = new QScrollBar(Qt::Vertical,this); 
  10.  
  11.     QHBoxLayout* layout = new QHBoxLayout(this); 
  12.     layout->addWidget(m_ListWidget); 
  13.     layout->addWidget(m_VScrollBar); 
  14.     layout->setMargin(0); 
  15.     layout->setSpacing(0); 
  16.     setLayout(layout); 
  17.  
  18.     connect(m_VScrollBar,SIGNAL(valueChanged(int)),this,SLOT(DisplayListView(int))); 
  19.     connect(this,SIGNAL(SendReSizeSignal(int,int)),m_ListWidget->GetList(),SLOT(ReSizeSlot(int,int))); 
  20. } 
  21.  
  22. Test::~Test() 
  23. { 
  24.  
  25. } 
  26. void Test::resizeEvent(QResizeEvent * event) 
  27. { 
  28.  
  29.     int width = this->size().width()-m_VScrollBar->width(); 
  30.     int column = width/WIDTH; 
  31.     column = width/WIDTH==0 ? 1 : column; 
  32.     int row = COUNT/column; 
  33.     row = COUNT%column==0 ? row : (row+1) ; 
  34.  
  35.     if(height()>=row*HEIGHT) 
  36.     { 
  37.         m_VScrollBar->setMinimum(0); 
  38.         m_VScrollBar->setMaximum(0); 
  39.         m_VScrollBar->setHidden(true); 
  40.     } 
  41.     else 
  42.     { 
  43.         m_VScrollBar->setHidden(false); 
  44.         int itemHeight = HEIGHT+GRAP; 
  45.         m_VScrollBar->setMinimum(0); 
  46.         int max = row*itemHeight-height(); 
  47.         int pageStep = height()-max; 
  48.         m_VScrollBar->setMaximum(max); 
  49.         m_VScrollBar->setPageStep(pageStep); 
  50.         m_VScrollBar->setSingleStep(10); 
  51.     } 
  52.  
  53.     int listViewHeight = row*(HEIGHT+GRAP); 
  54.     listViewHeight = listViewHeight > this->size().height() ? listViewHeight : this->size().height(); 
  55.     m_ListWidget->GetList()->resize(this->size().width(),listViewHeight); 
  56.  
  57.     emit SendReSizeSignal(row,column); 
  58.  
  59.     QWidget::resizeEvent(event); 
  60. } 
  61. void Test::DisplayListView(int len) 
  62. { 
  63.     m_ListWidget->GetList()->move(0,-len); 
  64. } 
  65. */  
  66.   
  67. Test::Test(QWidget *parent)  : QWidget(parent)  
  68. {  
  69.     m_ListWidget = new MyListWidget(this);  
  70.     m_VScrollBar = new QScrollBar(Qt::Vertical,this);  
  71.   
  72.     QHBoxLayout* layout = new QHBoxLayout(this);  
  73.     layout->addWidget(m_ListWidget);  
  74.     layout->addWidget(m_VScrollBar);  
  75.     layout->setMargin(0);  
  76.     layout->setSpacing(0);  
  77.     setLayout(layout);  
  78.   
  79.     connect(m_VScrollBar,SIGNAL(valueChanged(int)),this,SLOT(DisplayListView(int)));  
  80.     connect(this,SIGNAL(SendReSizeSignal(int,int)),m_ListWidget->GetList(),SLOT(ReSizeSlot(int,int)));  
  81. }  
  82.   
  83. Test::~Test()  
  84. {  
  85.   
  86. }  
  87. void Test::resizeEvent(QResizeEvent * event)  
  88. {  
  89.   
  90.     int width = this->size().width()-m_VScrollBar->width();  
  91.     int column = width/WIDTH;  
  92.     column = width/WIDTH==0 ? 1 : column;  
  93.     int row = COUNT/column;  
  94.     row = COUNT%column==0 ? row : (row+1) ;  
  95.   
  96.     if(height()>=row*HEIGHT)  
  97.     {  
  98.         m_VScrollBar->setMinimum(0);  
  99.         m_VScrollBar->setMaximum(0);  
  100.         m_VScrollBar->setHidden(true);  
  101.     }  
  102.     else  
  103.     {  
  104.         m_VScrollBar->setHidden(false);  
  105.         int itemHeight = HEIGHT+GRAP;  
  106.         m_VScrollBar->setMinimum(0);  
  107.         int max = row*itemHeight-height();  
  108.         int pageStep = height()-max;  
  109.         m_VScrollBar->setMaximum(max);  
  110.         m_VScrollBar->setPageStep(pageStep);  
  111.         m_VScrollBar->setSingleStep(10);  
  112.     }  
  113.   
  114.     int listViewHeight = row*(HEIGHT+GRAP);  
  115.     listViewHeight = listViewHeight > this->size().height() ? listViewHeight : this->size().height();  
  116.     m_ListWidget->GetList()->resize(this->size().width(),listViewHeight);  
  117.   
  118.     emit SendReSizeSignal(row,column);  
  119.   
  120.     QWidget::resizeEvent(event);  
  121. }  
  122. void Test::DisplayListView(int len)  
  123. {  
  124.     m_ListWidget->GetList()->move(0,-len);  
  125. }  
在编译的时候,一直出现链接 问题,把工程清理了,重新编译都不行,后来 把项目的生成的那一套 build-untitled-Desktop_Qt_5_3_MSVC2010_OpenGL_32bit_qt_53_w-Debug 整个文件夹给删除了,就可以了。看来QT中只要代码正确,保证头文件中的声明和实现中有定义,代码正确,就可以把生成的编译的东西删除,再编译,就可以了。原先一直在编译,然后一直不成功,刚开始一直怀疑是代码的问题,弄了好半天,最后原来是这个问题~~~~~~~~~~~~~~
0 0
原创粉丝点击