Mouse on a QLabel, show a text in another QLabel

  • Hi guys,
    i'm trying make this:
    when the mouse is on a qlabel i want to display in another qlabel, that i use to show the descriptions, the text in the whatthis property. I would like to have this for all the qlabels in the application. I've searched for similar case but i have not found anything. Thank you for help

  • Moderators

    @FED27 Easy to do, just override void QWidget::enterEvent(QEvent *event) in your "mouseover" label, then update the text in your "target" label.

  • Moderators

    @FED27 Hi, the method suggested by @ambershark is obviously the correct one, but requieres alot of work, especially if you want to have the whatthis of any and all Widget that is clicked upon.

    So I would suggest as an alternative,

    Hook into the MouseClicked or the MouseMove event of your main class, depending on when you want that Signal/Information

    than you can get the Widget with QWidget * QWidget::childAt(int x, int y) if that does not return a NullPtr, request the WhatsThis and send it to your Label.

  • Moderators

    @J-Hilk That's a good idea for what he wants to do with multiple widgets, otherwise he would have a lot of overridden classes. Good call.

    @FED27 To head off the next question if you use @J-Hilk's method, don't forget you need to turn on mouse tracking. That gets a lot of people wondering why they aren't receiving mouse movements. Make sure you call setMouseTracking(true) if you are going for mouse movement events in a widget like that. It's not required for the one I posted but definitely for full mouse movement tracking.

  • i try in this way:

    void MainWindow::mouseMoveEvent(QMouseEvent *event){

    QPoint pos=QCursor::pos();
    if(QWidget::childAt(pos.x(),pos.y())) ui->INFO->setText(QWidget::childAt(pos.x(),pos.y() )->whatsThis());


    it works but only if i click and after i move the mouse. I want always this property.

  • @ambershark
    UPDATE: it works only if i'm on the MainWindow of course.
    i need that some qlabel send whatthis() to a qlabel called "INFO" when mouse is over them.
    If i put mouseMoveEvent in the qlabel definition i don't know how connect them to INFO.

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)



    delete ui;

    void MainWindow::mouseMoveEvent(QMouseEvent *event){

    QPoint pos=mapFromGlobal(QCursor::pos());
    qDebug() << pos << endl;
    if(QWidget::childAt(pos.x(),pos.y())) ui->INFO->setText(QWidget::childAt(pos.x(),pos.y() )->whatsThis());
    else ui->INFO->clear();


  • Moderators

    @FED27 In that case I would also create a mouseMoveEvent in that QLabel that needs it. Then I would have your INFO widget accessible from your MainWindow so you can grab a pointer to it inside your QLabel.

  • Hi @FED27
    Write your own label class inherting QLabel, implement

    void QWidget::mouseMoveEvent(QMouseEvent *event){
    emit signal_data(this->text());

    emit signal with text of the label, connect signal to slot in target

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.