Customising Code of Qt Designer Widget

Customising code of Qt designer widget?

The most common way to solve this is by using widget promotion. This will allow you to replace a widget defined in Qt Designer with your own custom class. The steps for doing this are as follows:

In Qt Designer, select the QGraphicsView you want to replace, then right-click it and select Promote to... . In the dialog, set Promoted class name to "custom_gv", and set Header file to the python import path for the module that contains this class (e.g. "mypkg.widgets"). Then click Add, and Promote, and you will see the class change from "QGraphicsView" to "custom_gv" in the Object Inspector pane.

When the Qt Designer ui file is converted into PyQt code, it will automatically add an import statement like this:

from mypkg.widgets import custom_gv

and then in the converted code it will replace something like this:

    self.graphicsView = QtWidgets.QGraphicsView(MainWindow)

with this:

    self.graphicsView = custom_gv(MainWindow)

So the code in the ui file knows nothing about the custom class: it's just a name that is imported from elsewhere. That means you are completely free to write the custom class in any way you like.

In PyQt, this mechanism works in the same way with pyuic as it does with the uic module. The loadUi and loadUiType functions generate exactly the same code as pyuic does. The only difference is that the pyuic tool writes the generated code to a file, whereas the uic module loads it directly via exec.

How to create custom widget and use it in Qt Designer?

let me help you in order to get beautiful interface you need to learn CSS
I will show you how it works
that's what you have right now

Sample Image

This means that you do not correctly write the CSS code

QString textSheets = "QLabel,QLineEdit {width:60;height:20;max-width:60;max-height:20;;min-width:60;min-height:20;}";
QString widgetSheet = "customWidget01 {width:200;height:200;max-width:200;max-height:200;;min-width:120;min-height:200;}";
this->setStyleSheet(widgetSheet + textSheets); // does not work

I will exchange these lines for it

QString textSheets = "QLineEdit{ border-width: 2px; border-style: solid; border-color: red green black rgb(127,255,10); }"
"QLabel { border-width: 2px; border-style: solid; border-color: green black rgb(10,255,180) rgb(180,10,158); }" ;

setStyleSheet(textSheets);

and that's what result

Sample Image

to resize you just need to do so

//label1->setMinimumSize(150,50);
label1->setFixedSize(150,50);
//label1->setMaximumSize(150,50);
//label1->setMidLineWidth(150);

and that's what result

Sample Image

Custom widget in Qt Designer with pre-existing layout in frame

So I haven't worked with QT yet, but I'm going to gie it a try. First thing, I think, is that you should use the QStackedLayout to cover the widgets (source and QT manual). But you need to have a single stack.

So the stack should be a private member of CQFrame. E.g.

class CQFrame : public QFrame   
{
[...]
private:

QWidget* _frame; // Frame to cover.
QFrame* _frameCover;
QStackedLayout* _stackedLayout;
[...]

And probably:

CQFrame::~CQFrame()
{
delete _stackedLayout;
delete _frameCover;
}

Then you could already initialize everything in the constructor

CQFrame::CQFrame(QWidget* parent = 0, QWidget* frame)
: QFrame(parent)
, _frame(frame)
, _frameCover(new QFrame(this))
, _stackedLayout(new QStackedLayout(this))
{
_frameCover->setGeometry(rect());
[...]
_stackedLayout->addWidget(frame);
_stackedLayout->addWidget(frameCover);
//default:_stackedLayout->setStackingMode(QStackedLayout::StackOne);
}

You could then switch between widgets in the stack using

void CQFrame::SetCover(bool state)
{
if (state) _stackedLayout->setCurrentWidget(_frameCover);
else _stackedLayout->setCurrentWidget(_frame);
}

Maybe this helps you.

edit2:
I removed this code, as it was incorrect, both in coding format, as in idea
So I checked the QT sources QStackedLayout.cpp and QLayout and it seems a QWidget can have only one layout. If you add another layout, you get an error.
Furthermore, a QStackedLayout is a QLayout, is QObject. That could indicate it is automatically removed?

I'm not sure of the cover is implemented in QT as it should. It seems like you have a QWidget you want to cover, on top of which you put a QStackedLayout that is not used as designed i.e. switching stack objects, on top of which you put a new QWidget that is made visible or not.
And maybe that bottom QWidget is already in a (stacked)layout, etc.

How to use custom widget in Qt-Designer

There is a different example in the examples section of the Qt documentation that I think is a lot clearer.

Custom Widget Plugin Example

It was a little unclear to me when reading the tutorial where the Q_EXPORT_PLUGIN2() macro goes, but having full example code and project alleviates that.

Creating custom widget to be promoted in Qt designer

  • Place an empty widget where you want to have your FilesView
  • Right click on it and select Promote to
  • Set the promoted class name to FilesView press Add and then Promote
  • You cannot set the delegate from QtDesigner

For more info have a look here:

http://qt-project.org/doc/qt-4.8/designer-using-custom-widgets.html

The second option you have is to create a plugin for your widget which will allow you to set its properties through designer. If you are not going to use your widget multiple times I do not suggest it. For more details check the following link:

http://qt-project.org/doc/qt-4.8/designer-creating-custom-widgets.html



Related Topics



Leave a reply



Submit