QItemDelegate with custom view widget - qt

Qt 5.5 has a virtual method to define a custom widget for editing mode:
QWidget *createEditor(QWidget *parent,const QStyleOptionViewItem & option ,const QModelIndex & index ) const
But how to use a custom widget to override the "view" mode?
I saw "stars rating" delegate example where paint method is used but that's not what I need. I need to show a custom widget that contains other standard widgets inside it and use it in a view mode of QTableView or QListView. No need to get mess with painting pointers and figures - just show a custom widget (that has .ui file) and contains other standard widgets with their behaviour.
For example:
There is a download manager application that can show downloads either as a table or list view. QListView with a list of downloads. Each download has URL, Title, TotalSize, DownloadedSize, ProgressBar, Pause button, Remove button, Resume button. All of those can be columns in a table (QTableView) or composed similar to HTML's DIV in one cell (QListView widget)
How to achieve it? Is there anything like QWidget *createViewer(... ?
QtWidgets are used no QML.

For static content you can use QAbstractItemView::setIndexWidget.
For dynamic content the only option is to implement paint method in you delegate class.

Related

Qt paint on a widget created by QT Designer

How can I paint on a widget without overwriting the paintEvent.
I want to paint on a widget which is inside another one generated by Qt Designer , so i can't overwrite its paintEvent.
I tried to paint directly like this :
QPainter *painter= new QPainter(ui->drawArea);
painter.drawLine(50,50,50,150);
painter.close();
but Qt tell me that the QPainDevice is 0 or something like this,
I tried the same example by creating the painter then call the begin() method with the QPaintDevice (the widget) but same problem.
Qt version : 4.8.6.
Using custom widgets in Designer is not an issue. In Designer, add your widget as any other QWidget or QPushButton, depending which has the closest inheritance. Then with right click menu select Promote to ..., add your MyWidget.h and then promote the widget to MyWidget with reimplemented paintEvent(). Read more:
http://doc-snapshots.qt.io/4.8/designer-using-custom-widgets.html#promoting-widgets

How to make app looks nice in Qt?

I want to know is it possible to make application fully skinned/styled in Qt I mean by that not only controls inside the application window but the mainwindow itself! like close button/maximize button/minimize button and the bar, and the mainwindow border!, is it possible to do that in Qt? and how to?
Yes it is possible. The best method in Qt is to use Qt style sheets. The Qt documentation has plenty of examples and you can style all the widgets, either by specifying a whole class such as QPushButton, or a single, named widget.
As for the items on the title bar, I'm not sure if that's possible directly, but you could certainly turn off the main tool bar that contains the minimise / maximise buttons and then implement and style your own widgets while replicating their functionality.
The second argument to the QWidget constructor is Qt::WindowFlags. You can use the flags to control the properties of a window. For example, pass the flag Qt::FramelessWindowHint to create a borderless window.
There are a few ways to do this in code. You can use the setWindowsFlag method from within your widgets constructor:
setWindowFlags(Qt::FramelessWindowHint);
If you are creating a custom widget, you can pass the flag to QWidget's constructor:
YourWidget::YourWidget(QWidget *parent) : QWidget(parent, Qt::FramelessWindowHint)
{
// ....
}
Or you can pass the flag when you create a widget:
QWidget *your_widget = new QWidget(parent, Qt::FramelessWindowHint);
There are also flags for the minimize button (Qt::WindowMinimizeButtonHint), maximize button (Qt::WindowMaximizeButtonHint), close button (Qt::WindowCloseButtonHint), and title bar (Qt::WindowTitleHint). With these flags, you must also set Qt::CustomizeWindowHint to disable the defaults as described in the documentation.
See the flags documentation for a full list and additional details.
As #Merlin069 stated, style sheets allow you to control the look and feel of the widgets within your application.

QComboBox with QPushButtons

I would like to create a qt combo box with multiple columns containing push buttons and QLabel. If I look at the QSpinBox editor example, the editor is set only for one QSpinBox control. Can we create a complex control such as this and handle events?
Do you have any pointers?
QCombobox internally has a abstractitemview can be accessed by:
QAbstractItemView * QComboBox::view () const
Once you have a pointer to this view, you can define your own delegate for this view. This view draw the popups.
QAbstractItemView can have custom delegate, which can be a push button with QLabel or whatever you like.
Also, you may want to use this call to make delegate showing itself when view pop up:
openPersistentEditor ( const QModelIndex & index )
Here is a demo project QCombobox with button and spinbox exactly doing what you asking for.

How to use my treeview subclass in qt designer?

I have a class myTreeView which is a subclass of QTreeView, which I am using in other widget and doing layout manually. now I want to include myTreeView in the new widget using designer so that I can avoid layout code. any suggestions/reference, how to do this ?
Place a QTreeView into your layout in Qt Designer. Right click the QTreeView, click Promote to... add a New Promoted Class definition using the form at the bottom of the dialog.
i.e. specify the base class of your derived class as QTreeView, give the widget a name, and specify where Qt Design can find the header file for your derived class.
That should allow you, at a minimum, to place your widget on the form as you lay it out. It will most likely show up as a grey empty box (much like a QWidget) on the layout however when you compile and build a project using your .ui file your widget will appear.

How can I insert a widget into a mainwindow generated by Qt designer?

I have a Main window build with Qt Designer and I also have a widget built with Qt designer (both in a separate ui file). How can I instantiate my widget into my mainwindow at runtime?
The easiest way (using Designer) is to open your main window, drag a QWidget into it, and position/name the QWidget like you would your custom widget. Once that is done, right-click on the QWidget, and select Promote to.... A dialog will show up with the widgets it can be promoted to. At the bottom of that dialog, you can add a new widget for promotion. Type in the class name and include file information, and add that widget. Then select the entry in the list, and click the Promote button.
At the end of this process, you should be able to recompile, and your custom widget will be where you placed it in the main window.
Can't you use QMainWindow::setCentralWidget function?

Resources