Results 1 to 4 of 4

Thread: Loading 2 images in Same label ->

  1. #1
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Post Loading 2 images in Same label ->

    Qt Colleague's on a more senior level-(compared to me(beginning)

    I want to load 2 Images in the same label
    My Program entails:
    (spot the difference Game)
    Once a user clicks a point on 1 image & a corresponding point in the next image
    a line must be drawn between them.
    If I load the image in 2 separate labels, i'm not sure how I would draw a line between them accordingly, Though
    If I do load the images in separate labels, it will be easier to get coordinates to test if points match!

    pro's & cons...

    Ideas on how to approach this problem?
    link to an example, maybe
    Help appreciated

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Loading 2 images in Same label ->

    Loading two images on to QLable is not directly possible, you can use something like repeat pattern while loading the image, but as you already thought it is better to have two different QLabels.

    Using the point position and and relative methods of QWidget you can find out the point on QLabel. Here is an non-trival exmplae
    Qt Code:
    1. class Widget : public QWidget
    2. {
    3. public:
    4. explicit Widget(QWidget * parent = 0)
    5. : QWidget(parent)
    6. , mLeftLabel(new QLabel)
    7. , mRightLabel(new QLabel)
    8. , mTopWidget(new QWidget)
    9. , mBottomWidget(new QWidget)
    10. , mLeft()
    11. , mRight()
    12. {
    13. QGridLayout * gridLayout = new QGridLayout(mBottomWidget);
    14.  
    15. mLeftLabel->setPixmap(QPixmap("logo.png"));
    16. mRightLabel->setPixmap(QPixmap("logo.png"));
    17.  
    18. gridLayout->addWidget(mLeftLabel, 0, 0, 1, 1);
    19. gridLayout->addWidget(mRightLabel, 0, 1, 1, 1);
    20.  
    21. QStackedLayout * stackedLayout = new QStackedLayout(this);
    22.  
    23. stackedLayout->addWidget(mTopWidget);
    24. stackedLayout->addWidget(mBottomWidget);
    25. stackedLayout->setStackingMode(QStackedLayout::StackAll);
    26.  
    27. mTopWidget->installEventFilter(this);
    28. }
    29.  
    30. protected:
    31. void mousePressEvent(QMouseEvent * event)
    32. {
    33. QPoint w = event->pos();
    34. QPoint l = mLeftLabel->mapFromParent(w);
    35. QPoint r = mRightLabel->mapFromParent(w);
    36.  
    37. qDebug() << "Widget" << w;
    38.  
    39. if(mLeftLabel->rect().contains(l))
    40. {
    41. qDebug() << "Widget::mLeftLabel" << l;
    42. mLeft = w;
    43. update();
    44. }
    45.  
    46. if(mRightLabel->rect().contains(r))
    47. {
    48. qDebug() << "Widget::mRightLabel" << r;
    49. mRight = w;
    50. update();
    51. }
    52. }
    53.  
    54. bool eventFilter(QObject * object, QEvent * event)
    55. {
    56. if(object != mTopWidget)
    57. return false;
    58.  
    59. if(event->type() == QEvent::Paint)
    60. {
    61. QPainter painter(static_cast<QWidget*>(object));
    62.  
    63. QPen pen = painter.pen();
    64. pen.setWidth(5);
    65. pen.setColor(Qt::red);
    66. pen.setCapStyle(Qt::RoundCap);
    67. painter.setPen(pen);
    68.  
    69. painter.drawLine(mLeft, mRight);
    70. }
    71.  
    72. return false;
    73. }
    74.  
    75. private:
    76. QLabel * mLeftLabel;
    77. QLabel * mRightLabel;
    78.  
    79. QWidget * mTopWidget;
    80. QWidget * mBottomWidget;
    81.  
    82. QPoint mLeft;
    83. QPoint mRight;
    84. };
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    2lights (12th July 2013)

  4. #3
    Join Date
    Jun 2013
    Posts
    46
    Thanks
    24
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Exclamation Re: Loading 2 images in Same label ->

    Created the form in Qt Creator (Drag & Drop)
    2 labels besides each other & 1 new label (the size of the other 2)in front of them.
    Each Label is promoted to its own class(other calculations...)

    Works if I dont change the window size.
    But since I am changing window size! I have to set a layout on the form (so labels expands as window does)
    But then the overlapping label moves beside the other labels.

    How do I keep it above the other labels
    or rather how do i set it to resize accordingly without affecting the layout

    Or another way may
    How would I call a Paint function within 1 label to drawImage on leftside of label
    & draw the 2nd image on rightside

    Thanks
    Last edited by 2lights; 18th July 2013 at 08:34.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,728
    Thanks
    13
    Thanked 1,608 Times in 1,535 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Loading 2 images in Same label ->

    If you put them in a QStackedWidget the way Santosh suggests then the top widget will stay the correct size. You can do that with Designer if you wish.

    If you actually want to paint two separate images into the one QLabel then you need to subclass and reimplement paintEvent() to call drawPixmap() twice with different offsets and images.

Similar Threads

  1. Loading images from url in qt 4
    By shalini in forum Newbie
    Replies: 4
    Last Post: 15th September 2011, 16:21
  2. Replies: 0
    Last Post: 2nd August 2010, 11:17
  3. Replies: 6
    Last Post: 30th July 2010, 07:23
  4. How do i change images? Qt creator label>pixmap
    By QueenZ in forum Qt Programming
    Replies: 4
    Last Post: 8th February 2010, 04:44
  5. Slow loading images
    By abbapatris in forum Qt Programming
    Replies: 10
    Last Post: 5th March 2008, 15:52

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.