qstandarditemmodel

Delegate erasing text in QTreeView using QStandardItemModel

☆樱花仙子☆ 提交于 2019-12-11 18:12:46
问题 I'm having some difficulty adding a delegate to my QTreeView. I have added some QStandardItems through a model which works fine, but when I add the delegate, the text is erased and only the icons are visible. This is the code i'm using for my delegate: void SeqNavDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.column() == 0 && option.state & QStyle::State_Enabled) { const QIcon icon(QLatin1String(":/SeqNavMenu/images/green.png

Custom sorting method of QTableView?

落爺英雄遲暮 提交于 2019-12-11 09:28:48
问题 How can I setup custom sorting method for a QTableView , or for the model ? (Which function should I re-implement) The default sorting algorithm are for strings , I want a number sorting method for some specific column. Thanks. 回答1: You should use QSortFilterProxyModel. You should reimplement lessThan method. Then you have to set sourceModel for your proxy model, and set your proxy model as model for your view class MyProxyModel: public QSortFilterProxyModel { protected: bool lessThan ( const

QStandardItemModel inside Qtableview

倖福魔咒の 提交于 2019-12-11 08:49:12
问题 I am using QStandardItemModel inside QTableView . Here i have two button & Qtableview inside my mainwindow. I need only 4 columns inside this. And rows will vary. The two Buttons will be used to add/delete a row (test case). setHorizontalHeaderItem is not showing all the text(means all text is not visible). Example if i put 'Text for the Employee Name' it is not fully visible ? How to make QStandardItemModel occupy full QTableview ( width ). At present it is showing at top left corner ? How

QStandardItemModel::removeRows() does not work in my use case

丶灬走出姿态 提交于 2019-12-11 03:25:57
问题 Basically I want to delete all rows in my model. I'd prefer to use removeRows(..) instead of clear() , because I want to keep my headers. I guess I have missed something (docu here), my code is pretty simple: int c = MainWindow::_viewDataModel->rowCount(); bool r = MainWindow::_viewDataModel->removeRows(0, c); c e.g. is 4, but r is always false. I have tried 0 and 1 as first index. Using clear() works. The above code has no impact at all. 回答1: It is working now. After some testing my findings

How to Move Up a Selected Row in Qt

╄→гoц情女王★ 提交于 2019-12-10 18:54:22
问题 I have a QTableView with 3 rows and 2 columns. (Here I am using a QStandardItemModel ). I want to move up/move down a single row when a QPushButton is clicked. How can I move up/down a row in QTableView ? Thanks for your reply vahancho. I have already tried using QAbstractItemModel::moveRow , but it doesn't work: int currentRow = ui->tableView->currentIndex().row(); QModelIndex sourceParent = ui->tableView->model()->index(ui->tableView->selectionModel()->currentIndex().row(),0); QModelIndex

QListView & QStandardItemModel check text before editing row

让人想犯罪 __ 提交于 2019-12-08 04:24:22
问题 I want to check the text of a row in QListView before the user is editing it. If it doesn't fit a pattern, I don't want to accept it. Currently I have a QListView and QStandardItemModel . I can easily add and remove items via the QStandardItemModel . I also set the model of the list view. Are there some delegates or event function(s) on the list or the model for editing? 回答1: you can overload data() and setData() functions from QStandardItemModel , then when user tries to edit item your

Read and write to a file from a QTableView

时光总嘲笑我的痴心妄想 提交于 2019-12-05 03:56:36
问题 How can I read and write to a text file date enter to a QTableView? This is what I have but I would like to save the data when it is added to the table and of course be able to read it back when the application is reopened. Is there any tutorial I can refer to? MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); model = new QStandardItemModel(); model->setRowCount(0); ui->tableView->setModel(model); } MainWindow::~MainWindow() { delete ui

PyQT — How can you make a QTreeview uneditable but also selectable?

元气小坏坏 提交于 2019-12-04 22:51:23
问题 I just switched from wxPython to PyQT and I am having some trouble with the QTreeview. I have a QTreeview that will display data categorized into sections that are expandable, but the data in this TreeView should not be editable, but I need to be able to have the user select the data (doubleclicking is going to execute another method). I am not certain how to make it readonly but also selectable. I am using the QStandardItemModel with the QStandardItem to hold the data. Any help would be much

Read and write to a file from a QTableView

孤街浪徒 提交于 2019-12-03 22:17:20
How can I read and write to a text file date enter to a QTableView? This is what I have but I would like to save the data when it is added to the table and of course be able to read it back when the application is reopened. Is there any tutorial I can refer to? MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); model = new QStandardItemModel(); model->setRowCount(0); ui->tableView->setModel(model); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QStandardItem *userName = new QStandardItem(ui->lineEdit

PyQT — How can you make a QTreeview uneditable but also selectable?

流过昼夜 提交于 2019-12-03 13:43:42
I just switched from wxPython to PyQT and I am having some trouble with the QTreeview. I have a QTreeview that will display data categorized into sections that are expandable, but the data in this TreeView should not be editable, but I need to be able to have the user select the data (doubleclicking is going to execute another method). I am not certain how to make it readonly but also selectable. I am using the QStandardItemModel with the QStandardItem to hold the data. Any help would be much appreciated. You can set individual items to be uneditable by doing this when you create the