Qt paint on a widget created by QT Designer - qt

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

Related

How to add a UI QWidget to a UI QWidget?

Completely using QT Designer. I've created a QWidget to encapsulate a collection of controls. I now want to use this QWidget several times in the QMainWindow form.
How do I 'put' my QWidget onto the QMainWindow using QT Designer only?
Shouldn't this just be a simple drag and drop operation? What am I missing?
You can promote widgets.
Simply drag and drop QWidget into your mainwindow with QtDesigner.
Right click on it and press promote to In the dialog fill in your created widget's Class name (header should be filled in automatically if it doesn't match edit accordingly) press add and promote.
Now build and run your application and you should see your widget with collection of controls.

QItemDelegate with custom view widget

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.

Is is possible to edit an individual Widget in the QtDesginer?

I got a external library, which includes a derived class from QGLWidget, very similar to that one here. In that library I have a class:
class PictureGLWidget : public QGLWidget { //.. }
This extends Qt's native QGLWidget and personalizes it. But it was not written by me, I just got it, via a *.dll. So then, I bind that Widget manually in my code to a layout like:
QGridLayout* layout = new QGridLayout;
layout->addWidget(myPictureGLWidget, 0, 1);
ui->verticalLayout_5->addLayout(layout);
since I designed my MainWindowWidget with the integrated QtDesigner, which is by the way very comfortable, I would like to handle my myPictureGLWidget also in the QtDesigner, since I am currently redesigning the MainWindow.
Is there a way doing that? Thnx in advance!
Qt Designer supports any foreign widget class without needing to provide plugins for that. You only have to accept that the widget's properties and appearance won't be available within Designer.
Insert a dummy QWidget into the layout.
Right click on the widget, select "Promote to...".
Add PictureGLWidget as a new class promoted from QWidget. Specify appropriate header files etc.
Promote your widget to PictureGLWidget.
When this is done, the code generated by uic will instantiate a PictureGLWidget where you need it, instead of a dummy QWidget.
If you want to use the PictureGLWidget in the designer instead of a dummy widget, you can write a designer plugin that wraps the widget and exposes it in the widget pallette, provides property support, etc.
I might have misunderstood your question but don't you just add a QGLWidget to your design in Designer. Right click the widget and select Promote to... ?

How to detect if focus shift from a widget inside a QWidget to one outside?

I'm programming in Python using Qt with PySide and I have custom QWidget defined in a file called editor.py which is inserted in my ui in windowUi.py using the promotion method in the Qt Designer.
The custom QWidget class defined in editor.py doesn't do much besides using Elixir to edit items in a sqlite3 database and importing a ui file (editor.ui). Inside editor.ui there are a couple of QLineEdits and QDateTime widgets.
This widget is originally hidden in the main window and showed when needed. So far so good, but the problem is that I cannot make it hide when not needed. I decided that the widget is not needed when the user clicks anywhere else in the main window that is not the editor widget imported, that is, focus shift from the QWidget.
I looked upon this question: QWidget focusOutEvent not received and realized that the QWidget is really not getting focus.
If I call setFocusPolicy(StrongFocus) on it then I can make it hide if, and only if, the user clicks on the QWidget background (not on any widget inside it) and then clicks outside.
The question is then, how can I make it such that when the user clicks outside of this widget, shifting focus from any QLineEdit or QDateTime that's inside it to something else, the QWidget then hides itself?
Doesn't QApplication:::focusChanged ( QWidget * old, QWidget * now ) do what you need? You can check if QWidget *now is one of your QLineEdits/QDateTime or not (f.e. by going up by the QObject::parent)
Simply connect to this signal before showing and disconnect after hiding.

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