Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem when I try to use components of a form created with Qt Designer in C++ with hover elements like enterEvent or leaveEvent.
Forum Updated to NodeBB v4.3 + New Features

Problem when I try to use components of a form created with Qt Designer in C++ with hover elements like enterEvent or leaveEvent.

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 887 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fd_ntsi
    wrote on last edited by
    #1

    Hi,

    My problem is when I try to use components of a form created with Qt Designer in C++ with hover elements like enterEvent or leaveEvent.

    The form automatically created in Qt Designer is called "MainWindow".
    MainWindow has been instanciated for a multiple Inherance mode, so I do not use the "ui->".
    This form contains some QWidgets like QPushButtons and QLabels.
    One QPushButton is called "bouton".

    I can get or modify properties of bouton, for example :
    @bouton->setStyleSheet{"background-color: yellow"};@

    But what I want is to change the background-color of bouton when bouton is hovered by the mouse.
    (bouton is a .png image with transparency).

    I should write something in my mainwindow.cpp like:
    @void bouton::enterEvent(QEvent *event)
    {
    if(event->type() == QEvent::Enter) {
    bouton->setStyleSheet("background-color: rgb(240, 230, 140)");
    }
    }

    void bouton::leaveEvent(QEvent *event)
    {
    if(event->type() == QEvent::Leave) {
    bouton->setStyleSheet("background-color: transparent");
    }
    }@

    But bouton is not an object qualified by a class. It does not work.
    And I cannot create myself a class "bouton" because the compiler will consider a conflict with the mainwindow.ui

    The syntax for capturing the mouse event seems to be correct because changing void bouton by void MainWindow works, but naturally for the whole window ...

    So, I would appreciate any help !
    Thanks in advance.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Arnaut
      wrote on last edited by
      #2

      Hi,

      you can install an "event filter":http://qt-project.org/doc/qt-4.8/eventsandfilters.html#event-filters in your bouton object, but if all you want is a different background when the button is hovered then it can be easily done using the stylesheet "pseudo states":http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html#pseudo-states

      I hope it helps,
      H.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fd_ntsi
        wrote on last edited by
        #3

        Hi Arnaut
        Thanks.
        Yes it helps : writing in MainWindow.cpp :

        @ bouton->setStyleSheet(":hover {background-color: rgb(240, 230, 140)}");@

        Works exactly as I want !

        I was working on another way of solution, much more complicated, consisting in creating a class "Boutons", adding to it new properties, and then "promoting":http://qt-project.org/doc/qt-4.8/designer-using-custom-widgets.html bouton to the class Boutons.
        But your solution is more efficient for what I want to do.
        F.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved