Results 1 to 4 of 4

Thread: Showing an QImage doubleclicked from QMouseEvent inside a QLabel

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Showing an QImage doubleclicked from QMouseEvent inside a QLabel

    Hello!

    I never used QMouseEvent before and now I'm trying to learn how to do it, but till now I'm unable to be sucessfull. What I want to do is:

    I'm showing a movie (or else only one image) that consists in a series of images inside a QLabel that change over time due to a QTimer clock. Since the images shown are offently resized to fit in the QLabel's size, I want to add the possibility for the user to left double click in the image (or else in the QLabel, ether works) and a QDialog should appear, showing the imagem inside, while the movie should stop. When the QDialog is destroyed, the movie should continue.

    Till now what I did was essentially to copy from an example and the code almost worked. Here it is:

    Qt Code:
    1. void MainWindow::mousePressEvent(QMouseEvent *event)
    2. {
    3. QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
    4. if (!child)
    5. qDebug() << "Problem!!!!!";
    6. else if (event->button() == Qt::LeftButton)
    7. {
    8. timer.stop();
    9. QDialog qd;
    10. connect(&qd,SIGNAL(destroyed()),this,SLOT(restartTimer()));
    11. child->setParent(&qd);
    12. qd.exec();
    13. }
    14. }
    15.  
    16. void MainWindow::restartTimer()
    17. {
    18. timer.start(1000);
    19. }
    To copy to clipboard, switch view to plain text mode 

    The problem with this code is that the childAt() and setParent() functions seems not to "copy" the image inside the QLabel, but to TAKE it away - so while displaying the QDialog the QLabel in which was the image turns empty. As if that wasn't enough, when the timer restarts and its time for the new image to be shown, the software crash, since the QLabel in which the new image was supposed to appear was cut from its original place and put in the QDialog.

    No to mension that I wasn't able to understand how can I identify if the press event is a DoubleClick and at the same type if it is with the Left Button.


    So could somebody give me a typ, please?

    Thanks!


    Momergil

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Showing an QImage doubleclicked from QMouseEvent inside a QLabel

    Qt Code:
    1. connect(&qd,SIGNAL(destroyed()),this,SLOT(restartTimer()));
    2. child->setParent(&qd);
    3. qd.exec();
    To copy to clipboard, switch view to plain text mode 
    When exec() returns, qd goes out of scope. Since its QObject-based, it will attempt to delete all its childrens, including "child" QLabel.
    What you should do is to create new QLabel (or any other widget to display image) and show the image there, something like:
    Qt Code:
    1. timer.stop();
    2. QLabel * label = new QLabel(&qd);
    3. //...
    4. // create a layout or something, put the label there, set it to QDialog etc...
    5. //...
    6. if( const QPixmap * p = child->pixmap() )
    7. label->setPixmap(*p);
    8. connect(&qd,SIGNAL(destroyed()),this,SLOT(restartTimer()));
    9. qd.exec();
    To copy to clipboard, switch view to plain text mode 

    No to mension that I wasn't able to understand how can I identify if the press event is a DoubleClick and at the same type if it is with the Left Button
    use event->button() method.

  3. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Showing an QImage doubleclicked from QMouseEvent inside a QLabel

    Hello, stampede!

    Thanks for the tip; the code works.

    Now two problem remains:

    1) As I show in the first code, I did try to use "event->button()" method, but I couldn't find how to put it in the code so it would look after double clicks event. Anyway, I used "if (event->type() == QMouseEvent::MouseButtonDblClick)" and it worked.

    2) In my code, the images being shown in the original QLabel have being rezised; the originals are normally bigger. Now, when it comes about showing the QDialog, I want to show the originals, in theyr full, normal size, not as shown in the QLabel. Now how do I do this? Technically I must create an algorithm that somehow itendifies the name of the image being shown, look it again in the Resource files (where all the images are) and show that in the QDialog. Or is there another way of doing that?


    Thanks!

    Momergil


    Edit: Notice that I can't get the name of the image (the name as it is in the Resource file). here is how I get the image to put it in the original QLabel (actually, is only part of the conde, but enough to understand):

    Qt Code:
    1. QImage image(file.fileName());
    2.  
    3. l_images.push_back(image);
    4.  
    5. ui->Image->setPixmap(QPixmap::fromImage(newDimension(l_images[0])));
    6.  
    7.  
    8. QImage MainWindow::newDimension(QImage image)
    9. {
    10. nova_imagem;
    11.  
    12. if (image.height() > ui->Image->height())
    13. nova_imagem = image.scaledToHeight(ui->Image->height());
    14.  
    15. if (image.width() > ui->Image->width())
    16. nova_imagem = image.scaledToWidth(ui->Image->width());
    17.  
    18. return nova_imagem;
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Momergil; 18th October 2011 at 19:56. Reason: Note added

  4. #4
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Showing an QImage doubleclicked from QMouseEvent inside a QLabel

    News: I solved my own problem. Here is the code:

    Qt Code:
    1. void MainWindow::mousePressEvent(QMouseEvent *event)
    2. {
    3. if ((event->type() == QMouseEvent::MouseButtonDblClick) && (event->button() == Qt::LeftButton))
    4. {
    5. timer.stop();
    6. QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
    7.  
    8. if( const QPixmap * p = child->pixmap() )
    9. {
    10. QDialog dialog;
    11. connect(&dialog,SIGNAL(destroyed()),this,SLOT(restartTimer()));
    12. QLabel * label = new QLabel(&dialog);
    13.  
    14. QPixmap a = QPixmap::fromImage(imagemsemred);
    15. label->setPixmap(a);
    16. dialog.resize(a.size());
    17. dialog.exec();
    18. }
    19. }
    20. }
    21.  
    22. QImage MainWindow::newDimension(QImage image)
    23. {
    24. imagemsemred = image;
    25. nova_imagem = image;
    26.  
    27. if (image.height() > ui->Image->height())
    28. nova_imagem = image.scaledToHeight(ui->Image->height());
    29.  
    30. if (image.width() > ui->Image->width())
    31. nova_imagem = image.scaledToWidth(ui->Image->width());
    32.  
    33. return nova_imagem;
    34. }
    To copy to clipboard, switch view to plain text mode 

    The only problem is that I don't know what to do with that "p", since I don't use it as a variable but Its necessary for the software only allows mouse events over the Label.


    Thanks for everything,

    Momergil

Similar Threads

  1. Replies: 6
    Last Post: 27th December 2010, 19:26
  2. Replies: 0
    Last Post: 16th December 2010, 15:04
  3. QImage inside QFrame
    By HelderC in forum Newbie
    Replies: 5
    Last Post: 3rd September 2010, 18:36
  4. QLabel not showing
    By MarkoSan in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2008, 21:58
  5. ampersand showing in QLabel
    By dave in forum Newbie
    Replies: 6
    Last Post: 7th November 2006, 06:15

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.