Qt QTreeWidget save columns order - qt

How can I save the QTreeWidget column order ?
I know it can be done via QTreeView from this post
HowTo make restoreState() and saveState() work correctlly to QTableView class?
but I don't see how I can do it with QTreeWidget.
Any suggestions ?

A QTreeWidget is a QTreeView (it inherits it), so any function you can call on a QTreeView, you can also call on a QTreeWidget.
You can get the (only) header from a QTreeView (and therefore also from a QTreeWidget) using its QTreeView::header() member function. For example:
QTreeWidget *treeWidget = new QTreeWidget(this);
QByteArray saved = treeWidget->header()->saveState();

Related

What are the possible reasons why my subclass QTableWiget do not display?

I subclass a QTableWidget class and wanted to display in QTabWidget, something like this:
class Mainwindow:
_tabWidget->addTab(doc, QFileInfo(doc->fileName()).fileName());
class doc:
_tableWidget = new QTableWidget(row, column);
I can debug that "doc" is not NULL.
_tableWidget have values in rows and cells.
But all I can see on my application is the display of tab with correct label,
there are no rows and columns drawn.
I created the _tabWidget programmatically.
What are the reasons why the content of "doc" were not displayed?
Thanks in advance.
I tried to access the QTableWidget object of custom class from the Mainwindow class using a getter function (doc->table()):
_tabWidget->addTab(doc->table(), QFileInfo(doc->fileName()).fileName());
The rows and columns are now displayed.
Is it really the way to do it?
What you are doing in the little piece of code showing is creating a QTabWidget and adding a tab with a QWidget (base class of Doc) object. In your edit (that works), you are no longer adding the QWidget (doc), but adding a QTableWidget object (doc->table()) directly, which works.
I thus can only conclude that you are not setting a layout for QWidget and thus not putting the QTableWidget in the layout.

Push button for QTableView

I would like add QPushButtons to my QTableView. How can this be done with Qt? Is it possible to specify which column holds the button If I use QItemDelegate?
You've got to create your own PushButtonDelegate by subclassing QItemDelegate.
QAbstractItemView::setItemDelegateForColumn(int column, QAbstractItemDelegate * delegate) will set your delegate for the specified column of a view.
The implementation of the delegate depends on it's desired behavior. E.g. you can implement only createEditor(), setEditorData() and setModelData() to get the button to appear when the user starts editing a cell, or you can change the cell look completely by reimplementing the delegate's paint() method.
For more information see this. Also take a look at the Qt delegate examples.

I want to extend dropEvent() (QListWidget), should I completely reimplement it?

I have a QListWidget and I have set it to accept drops, the mode to be DragDrop etc and I can move QListWidgetItems all around the place (inside the QListWidget of course).
I like this behavior and I want it to let it as is, but I also want my QListWidget to accept drops from a QTreeWidget as well. If I attempt to reimplement the dropEvent() of my QListWidget then I lose the default behavior.
Is there any way to just extend the current behavior so as to have both drag-drop between listwidget items and drag-drop from the treewidget to the listwidget or I have to completely re-write both behaviors in my dropEvent() reimplementation?
Thanks a lot for any answers :)
Two ways:
implement a standalone event filter and make it act upon QEvent::Drop. Install it on your original QListWidget. Return false so that the original dropEvent() handler is called afterwards.
inherit from QListWidget, reimplement dropEvent(evt) and as a last statement, call QListWidget::dropEvent(evt);. Your original behavior will be retained.
No.
Subclass QListWidget, reimplement
virtual void QListWidget::dropEvent ( QDropEvent * event )
and explicitly call
QListWidget::dropEvent(event);
whenever you need the default behavior.
How to call a parent class function from derived class function?

How to set icon of the line edit in a QCombobox widget without inserting the item into the list?

I want to set the icon on the left side of the QCombobox widget. I know I can insert a item first and then set the icon of the inserted item and then select this newly inserted item. However, I would like to do that without inserting a new item into the drop down list for special reasons. Windows ComboBox control allows us to change the icon of the edit box by using an index of -1. I don't know how to achieve that with QCombobox.
Thanks for any comments!
Never tried it myself, but here is an idea.
QComboBox is based on Qt's model/view framework, so the items are contained into a QStandardItemModel which can be accessed with QComboBox::model().
The steps would be:
Instantiate a QStandardItem
Use setIcon() and setText() on the QStandardItem (or use the proper ctor)
When you want to add the item to the Combo list, append it thru the model.
Example:
QStandardItem* item = new QStandardItem(theIcon, theText);
[...]
QStandardItemModel* comboModel = qobject_cast<QStandardItemModel*>(theCombo->model());
comboModel->appendRow(item);

QListView how to add column?

How can I add columns to QListView control. Found a method addColumn while seardhing, but in my Qt Creator 1.2.1 based on Qt 4.5.2(32 bit) QListView doesn't have such method at all !!!
So how would I add columns ?
Say I have 3 columns then what is the code to add a row ?
You can use QTableView for this purpose. But if you need QListView look & feel, you can use QTableView borderless using Qt Stylesheet. Also you may want to add an icon. You can add icons to your QTableView by setting icon data to Qt::DecorationRole.
You cannot add a column, cause list views are designed to view simple lists. You need QTable[View/Widget].
QListWidget is a single column only. Use QTreeWidget/View for multiple columns.
As the start point you could watch how works QTableView class here: http://qt.nokia.com/doc/4.2/sql-tablemodel.html and do the similar things with QListView. So, you can't just emit addColumn() for QListView class, first you need to create model and then do listView->setModel(model).

Resources