qlistview

QListView show text when list is empty

最后都变了- 提交于 2019-12-01 19:09:27
I want to show some text (like "No items") when there are no items in QListView. I tried to override paintEvent method of QListView, but it doesn't have any effect. The code below shows a simple way of doing it by overloading the paintEvent method of the view. Painting of the text should probably use the style mechanism to obtain the font and pen/brush, but I'll leave that up for grabs by a keen editor. It uses Qt 5 and its C++11 features, doing it the Qt 4 or pre-C++11 way would require a QObject-deriving class with a slot to connect to the spin box's valueChanged signal. The implementation

QListView show text when list is empty

早过忘川 提交于 2019-12-01 18:02:04
问题 I want to show some text (like "No items") when there are no items in QListView. I tried to override paintEvent method of QListView, but it doesn't have any effect. 回答1: The code below shows a simple way of doing it by overloading the paintEvent method of the view. Painting of the text should probably use the style mechanism to obtain the font and pen/brush, but I'll leave that up for grabs by a keen editor. It uses Qt 5 and its C++11 features, doing it the Qt 4 or pre-C++11 way would require

QListView with QAbstractListModel shows an empty list

房东的猫 提交于 2019-12-01 04:43:14
I have created a very simple example of QListView with a custom QAbstractListModel . The QListView is displayed but it is empty. What am I doing wrong? Code: #include <QListView> #include <QAbstractListModel> #include <QApplication> class DataModel: public QAbstractListModel { public: DataModel() : QAbstractListModel() {} int rowCount( const QModelIndex & parent = QModelIndex() ) const { return 2; } QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const { return "a"; } }; int main( int argc, char **argv) { QApplication app(argc, argv, true); QListView *lv = new QListView(

QListView with QAbstractListModel shows an empty list

喜夏-厌秋 提交于 2019-12-01 02:12:36
问题 I have created a very simple example of QListView with a custom QAbstractListModel . The QListView is displayed but it is empty. What am I doing wrong? Code: #include <QListView> #include <QAbstractListModel> #include <QApplication> class DataModel: public QAbstractListModel { public: DataModel() : QAbstractListModel() {} int rowCount( const QModelIndex & parent = QModelIndex() ) const { return 2; } QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const { return "a"; } }

How to change the color of QStringListModel items?

走远了吗. 提交于 2019-11-30 19:47:29
I have QListView *myListView; QStringList *myStringList; QStringListModel *myListModel; which I fill with data like this: myStringList->append(QString::fromStdString(...)); myListModel->setStringList(*myStringList); myListView->setModel(myListModel); I want to change the font-color of some list entries, so I tried: for (int i = 0; i < myListModel->rowCount(); ++i) { std::cerr << myListModel->index(i).data().toString().toStdString() << std::endl; myListModel->setData(myListModel->index(i), QBrush(Qt::green), Qt::ForegroundRole); } The data is print out to cerr correctly, but the color does not

QListView,QTreeView和 QStandardItemModel的简单使用 (转...

我与影子孤独终老i 提交于 2019-11-30 11:19:32
#include "testqstandarditemmodel.h" #include <QtGui/QApplication> //int main(int argc, char *argv[]) //{ // QApplication a(argc, argv); // testQStandardItemModel w; // w.show(); // return a.exec(); //} #include <QApplication> #include <QWidget> #include<QTreeView> #include<QDirModel> #include<QHBoxLayout> #include<QSplitter> #include<QStringListModel> #include<QListView> #include<QStandardItemModel> //model有以下几种: // QStringListModel 存储一组字符串 // QStandardItemModel 存储任意层次结构的数据 // QDirModel 对文件系统进行封装 // QSqlQueryModel 对SQL的查询结果集进行封装 // QSqlTableModel 对SQL中的table进行封装 // QSqlRelationalTableModel

Signal when a QListView selection changes due to keyboard activity?

允我心安 提交于 2019-11-30 08:46:50
I have a QDialog, created with QT Designer, that looks like so: The list of servers on the left is a QListView with a QStringListModel. Mouse clicking an item in the list view updates the form with the information for the selected item by connecting the view’s activated(QModelIndex) signal to a slot function in the dialog. However, pressing up or down on the keyboard also changes the selected item, but no signal is emitted, so the form isn't updated to match the selected item. How can this be fixed? The activated(QModelIndex) signal actually refers to something more than just the act of

Render QWidget in paint() method of QWidgetDelegate for a QListView

好久不见. 提交于 2019-11-30 02:08:12
i'm having difficulties implementing custom widget rendering in a QListView . I currently have a QListView displaying my custom model called PlayQueue based on QAbstractListModel . This is working fine with simple text, but now I would like to display a custom widget for each element. So I subclassed a QStyledItemDelegate to implement the paint method like this: void QueueableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const { if (option.state & QStyle::State_Selected) painter->fillRect(option.rect, option.palette.highlight()); QWidget

How to get QString from QListView selected item in Qt?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 23:24:55
I need to get the selected item name in QListView as a QString . I have tried to google, but I haven't found anything useful. It depends on selectionMode lets say you have ExtendedSelection which means you can select any number of items (including 0). ui->listView->setSelectionMode(QAbstractItemView::ExtendedSelection); you should iterate through ui->listView->selectionModel()->selectedIndexes() to find indexes of selected items, and then call text() method to get item texts: QStringList list; foreach(const QModelIndex &index, ui->listView->selectionModel()->selectedIndexes()) list.append

Render QWidget in paint() method of QWidgetDelegate for a QListView

戏子无情 提交于 2019-11-28 23:03:53
问题 i'm having difficulties implementing custom widget rendering in a QListView . I currently have a QListView displaying my custom model called PlayQueue based on QAbstractListModel . This is working fine with simple text, but now I would like to display a custom widget for each element. So I subclassed a QStyledItemDelegate to implement the paint method like this: void QueueableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const { if (option