Qt QLabel Click Event

I am new to Qt and wonder.

I have objects QLabeland QLineEdit, and when the text is clicked QLabel, I want to set this text to QLineEdit.

I also read that I QLabeldid not click on the signal.

Can you explain how I can do this and write code for me ?!

+4
source share
3 answers

Either create another QWidget type, such as a specific QPushButton, to look like a QLabel and use its clicked () signal, or inherit QLabel yourself and emit your own click ().

See this example: https://wiki.qt.io/Clickable_QLabel

, . / QLabel QLineEdit :

QObject::connect(&label, SIGNAL(clicked(const QString& text)),
                 &lineEdit, SLOT(setText(const QString& text)));
+5

, , , - :

// main.cpp - this is a single-file example
#include <QtWidgets>

class MouseButtonSignaler : public QObject {
  Q_OBJECT
  bool eventFilter(QObject * obj, QEvent * ev) Q_DECL_OVERRIDE {
    if ((ev->type() == QEvent::MouseButtonPress
        || ev->type() == QEvent::MouseButtonRelease
        || ev->type() == QEvent::MouseButtonDblClick)
        && obj->isWidgetType())
      emit mouseButtonEvent(static_cast<QWidget*>(obj), 
                            static_cast<QMouseEvent*>(ev));
    return false;
  }
public:
  Q_SIGNAL void mouseButtonEvent(QWidget *, QMouseEvent *);
  MouseButtonSignaler(QObject * parent = 0) : QObject(parent) {}
  void installOn(QWidget * widget) {
    widget->installEventFilter(this);
  }
};

emit - , Qt :

#define emit

, moc . : - . - , moc - #include "main.moc" , moc () . . main.moc () void MouseButtonSignaler::mouseButtonEvent( .. ).

, QLabel:

int main(int argc, char ** argv) {
  QApplication app(argc, argv);
  MouseButtonSignaler signaler;
  QWidget w;
  QVBoxLayout layout(&w);
  QLabel label("text");
  QLineEdit edit;
  layout.addWidget(&label);
  layout.addWidget(&edit);
  signaler.installOn(&label);
  QObject::connect(&signaler, &MouseButtonSignaler::mouseButtonEvent, 
    [&label, &edit](QWidget*, QMouseEvent * event) {
    if (event->type() == QEvent::MouseButtonPress)
      edit.setText(label.text());
  });
  w.show();
  return app.exec();
}

#include "main.moc"
+2

Custom Label, QLabel. MouseButtonRelease, emit SLOT.

.h :

class YourLabelClass : public QLabel{

signals:
    void myLabelClicked();       // Signal to emit 

public slots:
    void slotLabelClicked();    // Slot which will consume signal 

protected:
    bool event(QEvent *myEvent); // This method will give all kind of events on Label Widget    
};

.cpp , :

YourLabelClass :: YourLabelClass(QWidget* parent) : QLabel(parent) {
   connect(this, SIGNAL(myLabelClicked()), this, SLOT(slotLabelClicked()));
}

event SLOT :

bool YourLabelClass :: event(QEvent *myEvent)  
{
    switch(myEvent->type())
    {        
        case(QEvent :: MouseButtonRelease):   // Identify Mouse press Event
        {
            qDebug() << "Got Mouse Event";
            emit myLabelClicked();
            break;
        }
    }
    return QWidget::event(myEvent);
}

void YourLabelClass  :: slotLabelClicked()   // Implementation of Slot which will consume signal
{
    qDebug() << "Clicked Label";
}

QLineEdit QLabel .

0

All Articles