PDA

View Full Version : [4.1.1] Custom Widget visible in Edit but not in Preview



moellney
19th March 2006, 14:11
Fedora Core 3, QT 4.1.1, gcc

Hi!

This is my first start in producing a custom widget: 'AnAusWidget' to use it in Designer 4.1.1.

The 'funny' thing is, that I can see my AnAusWidget in Editor mode, but when I go into preview mode the widget ist not there (kind of). AnAusWidget ist just a Widget produced in Designer holding one Button and a SpinBox with a simple connection between these two.

I tested two things to see, if it is really not there:

1. Into the Widget where I put in the AnAusWidget (by drag'n'drop) I put a QTextEdit above and one below and used VerticalLayout in the Widget to let the QTextEdit widgets expand sizing.
In Edit Mode I can see me AnAusWidget between the the two QTextEdits.
But wenn i go into Preview Mode the two QTextEdits expand into the area, where my AnAusWidget was located before..... is there a problem in the calculation how much space my AnAusWidgets wants?

2. I set the space for my AnAusWidget to FIXED/FIXED with space 200,200. Now in Preview the space is left free between the QTextEdits. Even the ToolTip for my AnAusWidget is show, but the AnAusWidget ist not.

Please see the attached ScreenShots

Please have a look at the code:


// File: an_aus.h
#include "ui_an_aus.h"
#include <QWidget>

class AnAusWidget : public QWidget
{

Q_OBJECT

public:
AnAusWidget(QWidget *parent = 0);
virtual QSize minimumSizeHint () const;

private:
Ui::an_aus _ui;
};




// File: an_aus.cpp
#include "an_aus.h"
#include "ui_an_aus.h"

AnAusWidget::AnAusWidget(QWidget *parent)
: QWidget(parent)
{
_ui.setupUi(this);
qDebug("Constructor of Widget was called");
qApp->beep();
}

QSize AnAusWidget::minimumSizeHint() const
{
return QSize(100,100);
qDebug("SizeHint was called");
}


The qDebug("Constructor ...") is called twice: start of dragging and after drop in edit mode. But never called in Preview... ?



// File: an_aus_plugin.h
#ifndef ANAUSPLUGIN_H
#define ANAUSPLUGIN_H

#include <QDesignerCustomWidgetInterface>

class AnAusPlugin : public QObject , public QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_INTERFACES(QDesignerCustomWidgetInterface)

public:
AnAusPlugin(QObject *parent = 0);

bool isContainer() const;
bool isInitialized() const;
QIcon icon() const;
QString codeTemplate() const;
QString domXml() const;
QString group() const;
QString includeFile() const;
QString name() const;
QString toolTip() const;
QString whatsThis() const;
QWidget *createWidget(QWidget *parent);
void initialize(QDesignerFormEditorInterface *core);

private:
bool initialized;

};

#endif




// File: an_aus_plugin.cpp
#include "an_aus_plugin.h"
#include "an_aus.h"

#include <QtPlugin>


AnAusPlugin::AnAusPlugin(QObject *parent)
: QObject(parent)
{
initialized = false;
}

void AnAusPlugin::initialize(QDesignerFormEditorInterfa ce *)
{
if (initialized) return;
initialized = true;
}

bool AnAusPlugin::isInitialized() const
{
return initialized;
}

QWidget *AnAusPlugin::createWidget(QWidget* parent)
{
return new AnAusWidget(parent);
}

QString AnAusPlugin::name() const
{
return "AnAus";
}

QString AnAusPlugin::group() const
{
return "Display Widgets [Examples]";
}

QIcon AnAusPlugin::icon() const
{
return QIcon();
}

QString AnAusPlugin::toolTip() const
{
return "";
}

QString AnAusPlugin::whatsThis() const
{
return "";
}

bool AnAusPlugin::isContainer() const
{
return false;
}

QString AnAusPlugin::domXml() const
{
// return "";

return "<widget class=\"AnAus\" name=\"an_aus\">\n"
" <property name=\"geometry\">\n"
" <rect>\n"
" <x>0</x>\n"
" <y>0</y>\n"
" <width>100</width>\n"
" <height>100</height>\n"
" </rect>\n"
" </property>\n"
" <property name=\"toolTip\" >\n"
" <string>AnAusSchalter</string>\n"
" </property>\n"
" <property name=\"whatsThis\" >\n"
" <string>Nur so mal ein Test "
"wie PLUGINs funktionieren.</string>\n"
" </property>\n"
"</widget>\n";

}

QString AnAusPlugin::includeFile() const
{
return "an_aus.h";
}

QString AnAusPlugin::codeTemplate() const
{
return "";
}

Q_EXPORT_PLUGIN2(customwidgetplugin, AnAusPlugin)





// File: an_aus_plugin.pro
FORMS = an_aus.ui
CONFIG += designer plugin debug_and_release
CONFIG += release
TEMPLATE = lib
HEADERS = an_aus.h \
an_aus_plugin.h
SOURCES = an_aus.cpp \
an_aus_plugin.cpp
target.path = $$[QT_INSTALL_PLUGINS]/designer
INSTALLS += target




To compile I used:
qmake an_aus_plugin.pro
make
su -c "make install"


I would be glad about any comment.

Kind regards,
Michael

rianquinn
20th March 2006, 19:48
I am having the same problem. Not sure why? Does anyone have a solution to this problem? My setup is the same .

rianquinn
20th March 2006, 22:35
I did some testing, and the custom widgets show when you compile and run a form using the custom widget, it simply doesn't show up in designer->preview

rianquinn
23rd March 2006, 12:56
You should be seeing two problems here. 1) Any container widget you create should not actually accept items to "contain." 2) All widgets should not work in Preview mode.

Here is the solution. Your widget naming scheme is not consistant.

in you doXml function, you use this:

return "<widget class=\"AnAus\" name=\"an_aus\">\n"" <property name=\"geometry\">\n"

when in fact it should be

return "<widget class=\"AnAusWidget\" name=\"AnAus\">\n"" <property name=\"geometry\">\n"

That class name should be the name of the class you are trying to make custom. More specifically, what ever you return in the creatWidget function, you should use here. In your case, you are returning a


AnAusWidget (parent)

so you class name should be


<widget class=\"AnAusWidget\"

Your name can be what ever you want, but it has to match what you put in the plugin::name function. You put


return "AnAus";

So your name in the doXml should be


name=\"AnAus\"

Finally, you should change this


Q_EXPORT_PLUGIN2(customwidgetplugin, AnAusPlugin)

to something like this:


Q_EXPORT_PLUGIN2(AnAus_v1, AnAusPlugin)

Make this as specfic as possible, otherwise Designer gets two of the same plugin "TARGET" names, and can't figure out which one to use.

I made the same exact mistakes, and this was the solution for me.

moellney
24th March 2006, 23:50
Dear rianquinn,

thank you for your information.

I tried you changes and the did no work. However they brougth me to the right track:

I had to change:


QString AnAusPlugin::name() const
{
return "AnAusWidget"; // b/c I create a AnAusWidget ... before was: return "AnAus";
}


and in
doXml



.....
return "<widget class=\"AnAusWidget\" name=\"an_aus\">\n"
.....


instead of


.....
return "<widget class=\"AnAus\" name=\"an_aus\">\n"
.....



So the main problem was, that I gave the wrong name of the custom widget at these two places.

:o

Thanks again for pointing me in the right direction.
Michael