PDA

View Full Version : Replacing QLabel with ImageViewer



rakefet
25th February 2014, 01:46
Hi,

I am asking the following question after looking at many threads and still could not figure out the way to go from here.
I created in QT creator a template of UI which contains multiple Qlabels. What I would like to do is instead of displaying an image at the QLabel, I would like to have a imageViewer widget. I have ImageViewer.cpp and ImageViewer.h from QT example. I just want to know the way to incorporate ImageViewer in my UI.
Here is the code for my UI.

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>725</width>
<height>734</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string/>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Pixel Coordinates:</string>
</property>
<property name="alignment">
<set>Qt::AlignHCenter|Qt::AlignTop</set>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="1" rowspan="2">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string/>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="0" rowspan="2">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string/>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox_5">
<property name="title">
<string/>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton_3">
<property name="text">
<string>Align Images</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_4">
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="1">
<widget class="QGroupBox" name="groupBox_4">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="title">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Pixel Coordinates:</string>
</property>
<property name="alignment">
<set>Qt::AlignHCenter|Qt::AlignTop</set>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

I'll appreciate any help.
Thanks

anda_skoa
25th February 2014, 09:29
Right click the label you want to replace, there is an option to "promote" it to another widget.
Add your class and promote. The type in the object tree should now be your class.

Cheers,
_

rakefet
25th February 2014, 20:36
Thanks. I used the promote feature to add ImageViewer to my widget as you can see from the screenshot:
10082

The error I am getting from ui_mainwindow.h is:
ImageViewer::ImageViewer(const ImageViewer &)': cannot convert parameter 1 from QGroupBox * to const ImageViewer &

and here is the lines generated by QT creater:
groupBox_image1 = new QGroupBox(centralWidget);
groupBox_image1->setObjectName(QStringLiteral("groupBox_image1"));
verticalLayout = new QVBoxLayout(groupBox_image1);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
widget_image1 = new ImageViewer(groupBox_image1);
widget_image1->setObjectName(QStringLiteral("widget_image1"));

What I am doing wrong?

Thanks.

anda_skoa
26th February 2014, 10:43
Check that your class has the usual QWidget constructor



class ImageViewer : public ....
{
Q_OBJECT
public:
explicit ImageViewer(QWidget *parent = 0);
};

i.e. a constructor that can be called with just the parent widget pointer.

Cheers,
_

rakefet
26th February 2014, 23:16
I am really sorry that I am still hassled by this, I have only been using QT for a week. I can build the project with no errors, however, no image is displayed. I am attaching the zipped files and I would really appreciate if somebody can tell me what the issue is.
10086
Thank you in advance.

anda_skoa
27th February 2014, 09:25
Your problem is that you are not showing the two image windows.

Your "MyWidget" class is a QMainWindow derived class, i.e. a top level window class. It will not show until you call show() on it.

You might want to change that to a normal QWidget, which is then embedded in your MainWindow class UI.

Cheers,
_

rakefet
27th February 2014, 13:45
Thanks for your inputs. The reason that I did not use QWidget for "MyWidget" is because I wanted to use the menu bar which I assume is available on a MainWindow class only. I tried to use this->show() at the end of my mywidget.cpp but the picture were shown on the top of my app and not inside the area that I designate it in my main app. As you suggested, I can always make my widget to be a QWidget class and give up on the menu bar.

Thanks.

anda_skoa
27th February 2014, 14:55
Thanks for your inputs. The reason that I did not use QWidget for "MyWidget" is because I wanted to use the menu bar which I assume is available on a MainWindow class only.

Yes, or at least QMainWindow is the one that makes it easiest. But usually the menu bar is on top of the window, not part of some embedded widget.
Do you really want two menu bars?


I tried to use this->show() at the end of my mywidget.cpp but the picture were shown on the top of my app and not inside the area that I designate it in my main app.

Yes, because by default QMainWindows are, well, windows.


As you suggested, I can always make my widget to be a QWidget class and give up on the menu bar.

Only giving up on a per-image menu bar.

If you really need two menu bars embedded into the main UI, then you need to reparent the MyWidget instances after creation.

But in my opinion that will look very weird, most applications do not have multiple menu bars somewhere deep inside their UI.

Cheers,
_

rakefet
27th February 2014, 15:08
That will be it. No menu bar for me.

Thank you for your help.