qt listtview 如何listview去掉滚动条条

QListView和QListWidget
QListView是基于Model,而QListWidget是基于Item。这是它们的本质区别。
往QListView中添加条目需借助QAbstractListModel:
MainWindow::MainWindow(QWidget *parent)
QMainWindow(parent),
ui(new Ui::MainWindow)
ui-&setupUi(this);
QStringListModel*
QStringListModel(this);
QStringList*
QStringList();
sl-&append("asdfsadfsa");
sl-&append("asdfsadfsa");
sl-&append("asdfsadfsa");
slm-&setStringList(*sl);
ui-&listView-&setModel(slm);
&&& delete
而在QListWidget中添加条目可以直接additem
& list.setViewMode(QListWidget::IconMode );
& list.setResizeMode(QListWidget::Adjust);
& list.setMovement(QListWidget::Static);
& for(int i =0; i & 100 ; i ++){
& QListWidgetItem *item = new
QListWidgetItem(&list);
& QStyle::StandardPixmap sp =
(QStyle::StandardPixmap)(i % 57);
& item-&setData(Qt::DecorationRole,
qApp-&style()-&standardPixmap(sp).scaled(QSize(16,16),
Qt::KeepAspectRatio, Qt::SmoothTransformation) );
item-&setData(Qt::DisplayRole,QObject::tr("Item
%1").arg(i));
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。评论-2092&
trackbacks-0
& & & & &Qt界面设计中有model-based的List View,有Item-based的List Widget,关于这2者到底有什么区别,暂时也没弄太明白,这些都是界面设计中的设计模式,从MVC发展而来。以后用到的时候自然会明白的。
&&&&&&&& 一般简单的都是用Item-based的List Widget,Qt中给出了List Widget,Tree Widget,Tabel Widget三种,这里就简单了解一下List Widget和Tree Widget的使用。Tabel Widget的使用应该也类似。
&&&&&&&& List Widget的使用
&&&&&&&& List Widget的使用比较简单,加入数据时直接用addItem()函数。读取数据时采用currentItem-&text()方法,本次实验先向List Widget中写入一些内容,然后单击button按钮时,以消息box的形式将选择行的内容显示出来。
&&&&&&&& 实验的效果如下:
实验代码及注释如下:
#include "dialog.h"
#include "ui_dialog.h"
#include &QtCore&
#include &QtGui&
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
ui-&setupUi(this);
//向listWdget中写入数据
for (int i = 10; i & 0; i--)
ui-&listWidget-&addItem("hello "+QString::number(i));
ui-&listWidget-&addItem("world"+QString::number(i));
Dialog::~Dialog()
void Dialog::on_pushButton_clicked()
//将数据读取出来并以消息box的形式显示出来。
QMessageBox::information(this, "clicked", ui-&listWidget-&currentItem()-&text());
&&&&&&&& Tree Widget的使用
&&&&&&&& Tree Wdiget中各个item是有层次关系的,也就是要区分父item和子item。这次实验是参考youtube中C++ Qt教程。完成的功能是在窗体的构造函数中新建几个item,包含了父item和子item,选中其中的一个item然后单击后会更改item里面文本的颜色。因此包含了Tree Widget的设置和读取2种简单的操作。
&&&&&&&& 效果如下:
程序代码和注释如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include &QtGui&
#include &QtCore&
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
ui-&setupUi(this);
//设置TreeWidget的列数
ui-&treeWidget-&setColumnCount(2);
//给每一列设置标签
ui-&treeWidget-&setHeaderLabels(QStringList() && "number" && "good");
addroot("one","very good");
addroot("two", "very good");
addroot("three", "very_good");
MainWindow::~MainWindow()
void MainWindow::addroot(QString name, QString description)
//从TreeWidget中构造出1个QTreeWidgetItem.
QTreeWidgetItem *item = new QTreeWidgetItem(ui-&treeWidget);
//向2列加入内容
item-&setText(0, name);
item-&setText(1, description);
//将该条目置顶,好像是置于下一个条目的底部
ui-&treeWidget-&addTopLevelItem(item);
//增加子条目
addchild(item, "here1", "very good1");
addchild(item, "here2", "very good2");
addchild(item, "here3", "very good3");
void MainWindow::addchild(QTreeWidgetItem *parent, QString name, QString dscription)
//这里QTreeWidgetItem构造函数时是不需要参数的,因为它被建立后就加载到parent上了
QTreeWidgetItem *item = new QTreeWidgetItem();
item-&setText(0, name);
item-&setText(1, dscription);
parent-&addChild(item);
void MainWindow::on_pushButton_clicked()
//如果是设置改变文字的颜色的话,那么当按钮按下后,需要把item中的条目焦点移开才能看到效果
ui-&treeWidget-&currentItem()-&setTextColor(0, Qt::green);
ui-&treeWidget-&currentItem()-&setTextColor(1, Qt::red);
//改变文本背景的颜色也是一样的效果
ui-&treeWidget-&currentItem()-&setBackgroundColor(0, Qt::green);
ui-&treeWidget-&currentItem()-&setBackgroundColor(1, Qt::red);
  总结:Item Widget在实际应用中比较广泛,其中Qt中的使用也很方便,可以将各种Item widget组合起来用。
阅读(...) 评论()
阿萨德发斯蒂芬QListView Class | Qt Widgets 5.6
QListView Class
class provides a list or icon view onto a model.
Header: #include &QListView&
qmake: QT += widgets Inherits:
Inherited By: , , and
Public Types
{ LeftToRight, TopToBottom }
{ SinglePass, Batched }
{ Static, Free, Snap }
{ Fixed, Adjust }
{ ListMode, IconMode }
Properties
: LayoutMode
: Movement
: ResizeMode
: ViewMode
16 properties inherited from
3 properties inherited from
6 properties inherited from
58 properties inherited from
1 property inherited from
Public Functions
(QWidget *parent = Q_NULLPTR)
int () const
Flow () const
QSize () const
bool (int row) const
bool () const
bool () const
LayoutMode () const
int () const
Movement () const
ResizeMode () const
void (int batchSize)
void (Flow flow)
void (const QSize &size)
void (LayoutMode mode)
void (int column)
void (Movement movement)
void (ResizeMode mode)
void (int row, bool hide)
void (bool show)
void (int space)
void (bool enable)
void (ViewMode mode)
void (bool on)
void (bool enable)
int () const
bool () const
ViewMode () const
bool () const
Reimplemented Public Functions
virtual QModelIndex (const QPoint &p) const
virtual void (const QModelIndex &index, ScrollHint hint = EnsureVisible)
virtual QRect (const QModelIndex &index) const
57 public functions inherited from
20 public functions inherited from
14 public functions inherited from
211 public functions inherited from
31 public functions inherited from
14 public functions inherited from
void (const QModelIndexList &indexes)
7 signals inherited from
3 signals inherited from
2 signals inherited from
Protected Functions
QRect (const QModelIndex &index) const
void (const QPoint &position, const QModelIndex &index)
Reimplemented Protected Functions
virtual void (const QModelIndex &current, const QModelIndex &previous)
virtual void (const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector&int& &roles = QVector&int& ())
virtual void (QDragLeaveEvent *e)
virtual void (QDragMoveEvent *e)
virtual void (QDropEvent *e)
virtual bool (QEvent *e)
virtual int () const
virtual bool (const QModelIndex &index) const
virtual void (QMouseEvent *e)
virtual void (QMouseEvent *e)
virtual QModelIndex (CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
virtual void (QPaintEvent *e)
virtual void (QResizeEvent *e)
virtual void (const QModelIndex &parent, int start, int end)
virtual void (const QModelIndex &parent, int start, int end)
virtual QModelIndexList () const
virtual void (const QItemSelection &selected, const QItemSelection &deselected)
virtual void (const QRect &rect, QItemSelectionModel::SelectionFlags command)
virtual void (Qt::DropActions supportedActions)
virtual void (QTimerEvent *e)
virtual void ()
virtual int () const
virtual QStyleOptionViewItem () const
virtual QSize () const
virtual QRegion (const QItemSelection &selection) const
virtual void (QWheelEvent *e)
37 protected functions inherited from
20 protected functions inherited from
4 protected functions inherited from
35 protected functions inherited from
9 protected functions inherited from
1 protected function inherited from
Additional Inherited Members
9 public slots inherited from
19 public slots inherited from
1 public slot inherited from
5 static public members inherited from
11 static public members inherited from
9 protected slots inherited from
1 protected slot inherited from
Detailed Description
class provides a list or icon view onto a model.
presents items stored in a model, either as a simple non-hierarchical list, or as a collection of icons. This class is used to provide lists and icon views that were previously provided by the QListBox and QIconView classes, but using the more flexible approach provided by Qt's model/view architecture.
class is one of the
and is part of Qt's .
This view does not display horizontal to display a list of items with a horizontal header, use
implements the interfaces defined by the
class to allow it to display data provided by models derived from the
Items in a list view can be displayed using one of two view modes: In , the items are displayed in the f in , the list view takes the form of an icon view in which the items are displayed with icons like files in a file manager. By default, the list view is in . To change the view mode, use the () function, and to determine the current view mode, use ().
Items in these views are laid out in the direction specified by the () of the list view. The items may be fixed in place, or allowed to move, depending on the view's () state.
If the items in the model cannot be completely laid out in the direction of flow, they can be wrapped at the boundar this depends on (). This property is useful when the items are being represented by an icon view.
The () and () govern how and when the items are laid out. Items are spaced according to their (), and can exist within a notional grid of size specified by (). The items can be rendered as large or small icons depending on their ().
list view.A
list view.A
list view.
Improving Performance
It is possible to give the view hints about the data it is handling in order to improve its performance when displaying large numbers of items. One approach that can be taken for views that are intended to display items with equal sizes is to set the
property to true.
See also , , , , and .
Member Type Documentation
enum QListView::Flow
ConstantValueDescription
QListView::LeftToRight0The items are laid out in the view from the left to the right.
QListView::TopToBottom1The items are laid out in the view from the top to the bottom.
enum QListView::LayoutMode
ConstantValueDescription
QListView::SinglePass0The items are laid out all at once.
QListView::Batched1The items are laid out in batches of
See also .
enum QListView::Movement
ConstantValueDescription
QListView::Static0The items cannot be moved by the user.
QListView::Free1The items can be moved freely by the user.
QListView::Snap2The items snap to the speci see ().
enum QListView::ResizeMode
ConstantValueDescription
QListView::Fixed0The items will only be laid out the first time the view is shown.
QListView::Adjust1The items will be laid out every time the view is resized.
enum QListView::ViewMode
ConstantValueDescription
QListView::ListMode0The items are laid out using
flow, with Small size and Static movement
QListView::IconMode1The items are laid out using
flow, with Large size and Free movement
Property Documentation
batchSize : int
This property holds the number of items laid out in each batch if layoutMode is set to Batched.
The default value is 100.
This property was introduced in
Access functions:
int batchSize() const
void setBatchSize(int batchSize)
This property holds which direction the items layout should flow.
If this property is , the items will be laid out left to right. If the
property is true, the layout will wrap when it reaches the right side of the visible area. If this property is , the items will be laid out from the top of the visible area, wrapping when it reaches the bottom.
Setting this property when the view is visible will cause the items to be laid out again.
By default, this property is set to .
Access functions:
Flow flow() const
void setFlow(Flow flow)
See also .
gridSize :
This property holds the size of the layout grid.
This property is the size of the grid in which the items are laid out. The default is an empty size which means that there is no grid and the layout is not done in a grid. Setting this property to a non-empty size switches on the grid layout. (When a grid layout is in force the
property is ignored.)
Setting this property when the view is visible will cause the items to be laid out again.
Access functions:
QSize gridSize() const
void setGridSize(const QSize &size)
See also .
isWrapping : bool
This property holds whether the items layout should wrap.
This property holds whether the layout should wrap when there is no more space in the visible area. The point at which the layout wraps depends on the
Setting this property when the view is visible will cause the items to be laid out again.
By default, this property is false.
Access functions:
bool isWrapping() const
void setWrapping(bool enable)
See also .
layoutMode :
This property determines whether the layout of items should happen immediately or be delayed.
This property holds the layout mode for the items. When the mode is
(the default), the items are laid out all in one go. When the mode is , the items are laid out in batches of
items, while processing events. This makes it possible to instantly view and interact with the visible items while the rest are being laid out.
Access functions:
LayoutMode layoutMode() const
void setLayoutMode(LayoutMode mode)
See also .
modelColumn : int
This property holds the column in the model that is visible.
By default, this property contains 0, indicating that the first column in the model will be shown.
Access functions:
int modelColumn() const
void setModelColumn(int column)
movement :
This property holds whether the items can be moved freely, are snapped to a grid, or cannot be moved at all.
This property determines how the user can move the items in the view.
means that the items can't be moved the user.
means that the user can drag and drop the items to any position in the view.
means that the user can drag and drop the items, but only to the positions in a notional grid signified by the
Setting this property when the view is visible will cause the items to be laid out again.
By default, this property is set to .
Access functions:
Movement movement() const
void setMovement(Movement movement)
See also , , and .
resizeMode :
This property holds whether the items are laid out again when the view is resized.
If this property is , the items will be laid out again when the view is resized. If the value is , the items will not be laid out when the view is resized.
By default, this property is set to .
Access functions:
ResizeMode resizeMode() const
void setResizeMode(ResizeMode mode)
See also , , and .
selectionRectVisible : bool
This property holds if the selection rectangle should be visible.
If this property is true then the selection
otherwise it will be hidden.
Note: The selection rectangle will only be visible if the selection mode is in a mode where more than one
i.e., it will not draw a selection rectangle if the selection mode is .By default, this property is false.
This property was introduced in
Access functions:
bool isSelectionRectVisible() const
void setSelectionRectVisible(bool show)
spacing : int
This property holds the space around the items in the layout.
This property is the size of the empty space that is padded around an item in the layout.
Setting this property when the view is visible will cause the items to be laid out again.
By default, this property contains a value of 0.
Access functions:
int spacing() const
void setSpacing(int space)
See also .
uniformItemSizes : bool
This property holds whether all items in the listview have the same size.
This property should only be set to true if it is guaranteed that all items in the view have the same size. This enables the view to do some optimizations for performance purposes.
By default, this property is false.
This property was introduced in
Access functions:
bool uniformItemSizes() const
void setUniformItemSizes(bool enable)
viewMode :
This property holds the view mode of the QListView.
This property will change the other unset properties to conform with the set view mode. -specific properties that have already been set will not be changed, unless () has been called.
Setting the view mode will enable or disable drag and drop based on the selected movement. For , the default movement is
(drag and drop disabled); for , the default movement is
(drag and drop enabled).
Access functions:
ViewMode viewMode() const
void setViewMode(ViewMode mode)
See also , , , , , and .
wordWrap : bool
This property holds the item text word-wrapping policy.
If this property is true then the item text is wrapped where necessary at word- otherwise it is not wrapped at all. This property is false by default.
Please note that even if wrapping is enabled, the cell will not be expanded to make room for the text. It will print ellipsis for text that cannot be shown, according to the view's .
This property was introduced in
Access functions:
bool wordWrap() const
void setWordWrap(bool on)
Member Function Documentation
QListView::QListView( *parent = Q_NULLPTR)
Creates a new
with the given parent to view a model. Use () to set the model.
QListView::~QListView()
Destroys the view.
void QListView::clearPropertyFlags()
Clears the -specific property flags. See .
Properties inherited from
are not covered by the property flags. Specifically,
are computed by
when calling () or ().
[virtual protected] void QListView::currentChanged(const
&current, const
&previous)
Reimplemented from ().
[virtual protected] void QListView::dataChanged(const
&topLeft, const
&bottomRight, const &int& &roles = QVector&int& ())
Reimplemented from ().
[virtual protected] void QListView::dragLeaveEvent( *e)
Reimplemented from ().
[virtual protected] void QListView::dragMoveEvent( *e)
Reimplemented from ().
[virtual protected] void QListView::dropEvent( *e)
Reimplemented from ().
[virtual protected] bool QListView::event( *e)
Reimplemented from ().
[virtual protected] int QListView::horizontalOffset() const
Reimplemented from ().
QListView::indexAt(const
Reimplemented from ().
[signal] void QListView::indexesMoved(const
This signal is emitted when the specified indexes are moved in the view.
This function was introduced in
[virtual protected] bool QListView::isIndexHidden(const
&index) const
Reimplemented from ().
bool QListView::isRowHidden(int row) const
Returns true if the row otherwise returns false.
[virtual protected] void QListView::mouseMoveEvent( *e)
Reimplemented from ().
[virtual protected] void QListView::mouseReleaseEvent( *e)
Reimplemented from ().
[virtual protected]
QListView::moveCursor( cursorAction,
modifiers)
Reimplemented from ().
[virtual protected] void QListView::paintEvent( *e)
Reimplemented from ().
[protected]
QListView::rectForIndex(const
&index) const
Returns the rectangle of the item at position index in the model. The rectangle is in contents coordinates.
See also ().
[virtual protected] void QListView::resizeEvent( *e)
Reimplemented from ().
[virtual protected] void QListView::rowsAboutToBeRemoved(const
&parent, int start, int end)
Reimplemented from ().
[virtual protected] void QListView::rowsInserted(const
&parent, int start, int end)
Reimplemented from ().
[virtual] void QListView::scrollTo(const
hint = EnsureVisible)
Reimplemented from ().
[virtual protected]
QListView::selectedIndexes() const
Reimplemented from ().
[virtual protected] void QListView::selectionChanged(const
&selected, const
&deselected)
Reimplemented from ().
[protected] void QListView::setPositionForIndex(const
&position, const
Sets the contents position of the item at index in the model to the given position. If the list view's movement mode is Static or its view mode is , this function will have no effect.
This function was introduced in
void QListView::setRowHidden(int row, bool hide)
If hide is true, the given row otherwise the row will be shown.
See also ().
[virtual protected] void QListView::setSelection(const
Reimplemented from ().
[virtual protected] void QListView::startDrag( supportedActions)
Reimplemented from ().
Reimplemented from ().
Reimplemented from ().
[virtual protected] int QListView::verticalOffset() const
Reimplemented from ().
[virtual protected]
QListView::viewOptions() const
Reimplemented from ().
[virtual protected]
QListView::viewportSizeHint() const
Reimplemented from ().
This function was introduced in
QListView::visualRect(const
&index) const
Reimplemented from ().
[virtual protected]
QListView::visualRegionForSelection(const
&selection) const
Reimplemented from ().
Since 4.7, the returned region only contains rectangles intersecting (or included in) the viewport.
[virtual protected] void QListView::wheelEvent( *e)
Reimplemented from ().
& 2016 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.
The documentation provided herein is licensed under the terms of the
as published by the Free Software Foundation.
Qt and respective logos are trademarks of The Qt Company Ltd.
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners.

我要回帖

更多关于 去掉listview的滚动条 的文章

 

随机推荐