July 12, 2012

Vikuseth Vikuseth
Ant Farmer
76 posts

Handling mouseevent in Qstatusbar .

 

In my statusbar , i have added one Qlabel inside which i am displaying some message . Now what i want is when i click on that Qlabel(present inside the Qstatusbar ) , the message should disappear ..

I have added the label inside statusbar as follows ,

  1. QLabel *cpyrightlbl= new QLabel();
  2.  ui.statusBar->addWidget(cpyrightlbl);
  3.  cpyrightlbl->setText("Demo Message");
  4.  cpyrightlbl->setWindowFlags( Qt::FramelessWindowHint );
  5.  cpyrightlbl->setStyleSheet("border: 3px");
  6.  cpyrightlbl->setFixedWidth(frameGeometry().width());
  7.              cpyrightlbl->show();

7 replies

July 12, 2012

Sam Sam
Area 51 Engineer
628 posts

Why do you want to use a mouseClick on the status bar message.. You can also dispay the message for a particular time interval and it will disappear after timeout.

check this [qt-project.org]

July 12, 2012

Vikuseth Vikuseth
Ant Farmer
76 posts

Actually the requirement is unless the user clcik on that label , the message shall be displayed .

July 12, 2012

Lukas Geyer Lukas Geyer
Dinosaur Breeder
2074 posts

Subclass QLabel and reimplement mousePressEvent().

July 12, 2012

Vikuseth Vikuseth
Ant Farmer
76 posts

i have done as u said above , but it has no impact ..

Here i am mentioning my code

inside display.h

  1. #ifndef MSGDISPLAY_H
  2. #define MSGDISPLAY_H
  3.  
  4. #include <QLabel>
  5.  
  6. class MsgDisplay : public QLabel
  7. {
  8.  Q_OBJECT
  9. public:
  10.  MsgDisplay(QWidget *parent = 0);
  11.  ~MsgDisplay();
  12.  void setMessage(QString str);
  13. protected:
  14.  virtual void mousePressEvent(QMouseEvent * event);
  15. };
  16.  
  17. #endif // MSGDISPLAY_H
  18.  
  19. inside display.cpp
  20.  
  21. #include "MsgDisplay.h"
  22.  
  23. MsgDisplay::MsgDisplay(QWidget *parent)
  24.  : QLabel(parent)
  25. {
  26.  
  27. }
  28. void MsgDisplay::setMessage(QString str)
  29. {
  30.  setText("<font color='Blue'>Demo Message<font>");
  31.  QFont font;
  32.  font.setPointSize(10);
  33.  font.setBold(true);
  34.  setFont(font);
  35.  
  36. }
  37. MsgDisplay::~MsgDisplay()
  38. {
  39.  
  40. }
  41. void MsgDisplay::mousePressEvent(QMouseEvent * event)
  42. {
  43.  hide();
  44. }
  45.  
  46. inside Myclass.cpp
  47.  
  48. #include display.h
  49.  
  50. void Myclass::Label()
  51. {
  52.             MsgDisplay *statusLabel = new MsgDisplay();
  53.  ui.statusBar->addWidget(statusLabel);
  54.  statusLabel->setMessage(QString("hi"));
  55. }

July 12, 2012

Lukas Geyer Lukas Geyer
Dinosaur Breeder
2074 posts

The code you’ve posted should work as expected (at least when it comes to the requested functionality). Can you provide a small, compilable example which reproduces the problem?

July 12, 2012

Vikuseth Vikuseth
Ant Farmer
76 posts

I got the solution for the above one ..
Now i am running through one more problem i.e when i minimize my main application window the status bar message is getting truncated . Is there any way to resolve this issue ? I mean to say the same message shall be displayed without any trucation during minimizing and maximizing .

July 12, 2012

Andre Andre
Area 51 Engineer
6075 posts

How do you imagine to display anything on a minimized application window?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ [solved] Modify form through main.cpp      [Solved] QSlider throws segmentation fault on create ››

You must log in to post a reply. Not a member yet? Register here!