注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沙漠里de烟雨

原创分享,禁止转载

 
 
 

日志

 
 

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

2014-01-06 13:03:18|  分类: QT5.x与QML |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
效果图:
Qt---QListView中放置自定义控件并添加滚动条 - 漠雨 - 沙漠里de烟雨
 

 以下是各个文件的代码:
------------------------------Enum.h == >>>>如下:

#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

----------------------------main.cpp==>>>>如下:

#include "test.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test w;
w.show();
return a.exec();
}


----------------------------------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


-----------------------------------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);
}


-------------------------MyListWidget.h======>>>>>>如下:

#ifndef LIST_H
#define LIST_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 // LIST_H


--------------------------------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;
}


--------------------------------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


----------------------------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;
}


------------------------------MyWinBox.h=====>>>>>如下:

#ifndef MYBOX_H
#define MYBOX_H

#include <QWidget>
#include <QLineEdit>

class MyWinBox : public QWidget
{
Q_OBJECT

public:
MyWinBox(QWidget *parent);
~MyWinBox();

public:
QLineEdit* m_NameEdit;
};

#endif // MYBOX_H


--------------------------------------------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()
{

}


具体的实现原理在上一篇博文<< 给控件增加滚动条之--QScrollArea  >>中的结尾处提及,这里就不再多述。
  评论这张
 
阅读(14484)| 评论(1)
推荐

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017