QT 5.3 QListView column problems - c++

guys! I'm newbie in QT Programming.
I want to ask, why columns is appearing as usual row on my QListView control?
QStandardItemModel* model = new QStandardItemModel(0, 1, ui->listView);
QList<QStandardItem*> items;
items.append(new QStandardItem("Column 1"));
model->insertColumn(0, items);
ui->listView->setModel(model);

QListView simply can not display multiple columns. Use QTableView.

Related

Add QIcon to QTableView cell

I have already coded this for QTableWidget:
void ReadOnlyWindow::addReportIconToRow(const int rowIndex){
QIcon icon;
QSize sz(16, 16);
icon.addPixmap(style()->standardIcon(QStyle::SP_FileDialogEnd).pixmap(sz), QIcon::Normal);
QTableWidgetItem *iconItem = new QTableWidgetItem();
iconItem->setText("report");
iconItem->setIcon(icon);
iconItem->setFlags(iconItem->flags() & (~Qt::ItemIsEditable));
ui->homeWorksTable->setItem(rowIndex, REPORT_COLUMN_INDEX, icon);
}
REPORT_COLUMN_INDEX is const int from class and it has value 4.
I am trying to found out how to rewrite code if table is `` QTableView`.
I was trying to use setItemData() and setData() but I think I used it in bad way because it didn't work.
P.S.: Now I want to do it for QTableView because its easy to load SQLite table there. This part works. I also added one more column. Now I need add to all rows of this column icon with text (how is in my code for QTableWidget). Function up there should be for one cell and will be implemented in loop.
to add an icon to a tableWidget you set the item like specified here :
and when defining the QTableWidgetItem, use the constructor taking an Icon to be displayed.
here is a short example:
this->ui->myTable->setItem(row, col, new QTableWidgetItem(QIcon(":/resources_to_icon_.png"),"SomeText"));
in your code:
ui->homeWorksTable->setItem(rowIndex, REPORT_COLUMN_INDEX, QTableWidgetItem(icon,"some text");

Qt TreeView problem when used with sort model

I have a problem where i did not find a solution so far, actually the question is when and for what QModelIndex we have to call beginInsertColumns and similar functions.
My example is the standard Qt example from here:
http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html
It is also delivered with every qt installation, e.g. C:\Qt\Qt5.12.0\Examples\Qt-5.12.0\widgets\itemviews\editabletreemodel
When just executed, it works fine, but now lets insert a QSortFilterProxyModel, just to make it sortable. We change the MainWindow c'tor to
...
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel *model = new TreeModel(headers, file.readAll());
file.close();
QSortFilterProxyModel* sort = new QSortFilterProxyModel( this );
sort->setSourceModel( model );
view->setModel( sort );
...
Now execute the example, expand one or two of the root nodes and select the menu entry "insert column" you will see, that only the root nodes have a new column, all their children do not have the new column. It works without the proxy. Now my question, do i have to emit beginInsertColumns for every QModelIndex that has children?
How can i fix this?
Note, this is not only a problem with qt 5.12, but also with many earlier releases.
I've always had problems using QSortFilterProxyModel, and as far as I understood they are often related to the index mapping between with the source model.
I managed to solve it using the QSortFilterProxyModel::invalidate() method that will reset the mapping, without spoiling the current state of the view.
Modify the MainWindow::insertColumn as follows:
bool MainWindow::insertColumn()
{
QAbstractItemModel *model = view->model();
int column = view->selectionModel()->currentIndex().column();
bool changed = false;
// Insert a column in the parent item.
QSortFilterProxyModel* sfpm = qobject_cast<QSortFilterProxyModel*>(model);
if (sfpm)
{
changed = sfpm->insertColumn(column + 1);
if (changed)
sfpm->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"), Qt::EditRole);
sfpm->invalidate();
}
updateActions();
return changed;
}
It is not an optimal solution, and I don't know if there is a better way, but works for me.

QTableWidget, centering cellWidgets

Is there a way to place QCheckBox as a cell widget of QTableWidget in the center of a cell, not at the left side, without additional QWidget and adding the checkbox to it's layout?
Use setCellWidget to add QCheckBox to table:
QWidget *checkBoxWidget = new QWidget(); //create QWidget
QCheckBox *checkBox = new QCheckBox(); //create QCheckBox
QHBoxLayout *layoutCheckBox = new QHBoxLayout(checkBoxWidget); //create QHBoxLayout
layoutCheckBox->addWidget(checkBox); //add QCheckBox to layout
layoutCheckBox->setAlignment(Qt::AlignCenter); //set Alignment layout
layoutCheckBox->setContentsMargins(0,0,0,0);
ui->tableWidget->setCellWidget(0,0, checkBoxWidget);
Also use these line to resize to contents:
ui->tableWidget->resizeRowsToContents();
ui->tableWidget->resizeColumnsToContents();
setCellWidget: Sets the given widget to be displayed in the cell
in the given row and column, passing the ownership of the widget to
the table.
Reference: https://evileg.com/en/post/79/
I have a simple solution without any additional layout if you want to use ui->tableWidget->resizeColumnsToContents():
Use checkBox->setStyleSheet( "text-align: center; margin-left:50%; margin-right:50%;" );
AFTER this, call ui->tableWidget->resizeColumnsToContents().
If the column is resized after this (eg. the table gets resized by the layout), you may have to call ui->tableWidget->resizeColumnsToContents() again.
It the widget is placed before the resulting column width is known, it retains its (wrong) position. Using resizeColumnsToContents() repositions it.

assigning model to tableview and creating rows and columns

I am New to Qt creator .
I want to create a table View with 1 row and 2 columns using QmodelIndex.
There was an error in assigning the model to the tableView and creating data .
QTableView* const tableView
= { htca_ui->tableView,
};
tableView->setModel(&mymodel);
How to create a model and assign to the view to create the row with 2 columns using QmodelIndex?
Please Help
If you have added a tableView with the UI designer you don't need to create it again in code. Assuming your QTableView is called tableView and that htca_ui is your UI pointer, you can do
htca_ui->tableView->setModel(&mymodel);
Edit : And it occurs to me the &mymodel means you're creating it on the stack, which may not be the best idea. Better to do :
MyModel *mymodelinstance = new MyModel(this);
htca_ui->tableView->setModel(mymodelinstance);
However: the model classes can be complex to work with. If you are just after a simple table with minimum code, I would suggest using a QTableWidget rather than a QTableView. Add a QTableWidget to your UI, then you can do
htca_ui->tableWidget->setRowCount(1);
htca_ui->tableWidget->setColumnCount(2);
QTableWidgetItem *item = new QTableWidgetItem("An Item");
htca_ui->tableWidget->setItem(0,0, item); // The item at the top left will read "An Item".
See the documentation for QTableWidget for more info

QT QTableWidget not appearing in QTabWidget

I have an application which uses multiple tabs. I used QTabWidget. On some tabs I needed to show tables, so I used QTableWidget.
The code snippet is:
QWidget *qwgt = qPreviewTabs->widget(Index);
QTableWidget *qDrvTab = new QTableWidget();
....
....
....
QVBoxLayout *vbLyt = new QVBoxLayout();
vbLyt->addWidget(qDrvTab);
qwgt->setLayout(vbLyt);
When I add push buttons and tree widgets they all appear on the specified tab without any problem. Only the QTableWidget refuses to show.
A table with no rows and columns is a void.
So do
qDrvTab->setRowCount(no_of_rows);
qDrvTab->setColumnCount(no_of_cols);
before adding it to layout.
Now you can see your Tablewidget in layout.