Results 1 to 9 of 9

Thread: Replacing QLabel with ImageViewer

  1. #1
    Join Date
    Feb 2014
    Posts
    26
    Qt products
    Qt5
    Platforms
    Windows

    Default Replacing QLabel with ImageViewer

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Replacing QLabel with ImageViewer

    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,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    26
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Replacing QLabel with ImageViewer

    Thanks. I used the promote feature to add ImageViewer to my widget as you can see from the screenshot:
    test1.jpg

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Replacing QLabel with ImageViewer

    Check that your class has the usual QWidget constructor

    Qt Code:
    1. class ImageViewer : public ....
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit ImageViewer(QWidget *parent = 0);
    6. };
    To copy to clipboard, switch view to plain text mode 
    i.e. a constructor that can be called with just the parent widget pointer.

    Cheers,
    _

  5. #5
    Join Date
    Feb 2014
    Posts
    26
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Replacing QLabel with ImageViewer

    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.
    test22.zip
    Thank you in advance.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Replacing QLabel with ImageViewer

    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,
    _

  7. #7
    Join Date
    Feb 2014
    Posts
    26
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Replacing QLabel with ImageViewer

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Replacing QLabel with ImageViewer

    Quote Originally Posted by rakefet View Post
    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?

    Quote Originally Posted by rakefet View Post
    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.

    Quote Originally Posted by rakefet View Post
    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,
    _

  9. #9
    Join Date
    Feb 2014
    Posts
    26
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Replacing QLabel with ImageViewer

    That will be it. No menu bar for me.

    Thank you for your help.

Similar Threads

  1. Replies: 0
    Last Post: 9th November 2010, 07:58
  2. How to deploy the imageviewer example in windows?
    By fork in forum Installation and Deployment
    Replies: 4
    Last Post: 11th March 2010, 10:52
  3. Replies: 1
    Last Post: 29th September 2009, 19:44
  4. Storage and View -> imageViewer
    By C167 in forum Qt Programming
    Replies: 15
    Last Post: 22nd January 2008, 14:43

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.