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. Two-widgets stacked widget switching when mouse hovers it.
QtWS25 Last Chance

Two-widgets stacked widget switching when mouse hovers it.

Scheduled Pinned Locked Moved Solved General and Desktop
stackedwidgethover
6 Posts 2 Posters 551 Views
  • 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.
  • D Offline
    D Offline
    dauriac
    wrote on 29 Mar 2023, 13:54 last edited by
    #1

    I would like to create a 2-widgets stacked widget with the following property:
    if the mouse hovers the stacked widget then the current index is set to 0
    else the current index is set to 1
    The following code produce SIGSEGV. Note also that this->setCurrentIndex(1) in the constructor is ineffective.

    <code>
    MyQStackedWidget::MyQStackedWidget(QWidget *parent) :
        QStackedWidget(parent)
    {
        setAttribute(Qt::WA_Hover);
        fprintf(stderr,"Creator Stacked\n");
        this->setCurrentIndex(1);  // THIS HAS NO EFFECT !!
    }
    
    bool MyQStackedWidget::event(QEvent* e)
    {
        switch(e->type())
        {
        case QEvent::HoverEnter:
            hoverEnter(static_cast<QHoverEvent*>(e));
            return true;
        case QEvent::HoverLeave:
            hoverLeave(static_cast<QHoverEvent*>(e));
            return true;
        default:
            break;
        }
        return QWidget::event(e);
    }
    
    void MyQStackedWidget::enterEvent(QEvent* e)
    {
        fprintf(stderr,"enterEvent MyQStackedWidget\n");
    }
    
    void MyQStackedWidget::leaveEvent(QEvent* e)
    {
        fprintf(stderr,"leaveEvent MyQStackedWidget\n");
    }
    
    void MyQStackedWidget::hoverEnter(QHoverEvent* event)
    {
        fprintf(stderr,"hoverEnter MyQStackedWidget\n");
        int i = (this->currentIndex()+1)%2;
        this->setCurrentIndex(i);
    }
    
    void MyQStackedWidget::hoverLeave(QHoverEvent* event)
    {
        fprintf(stderr,"hoverLeave MyQStackedWidget\n");
        int i = (this->currentIndex()+1)%2;
        this->setCurrentIndex(i);
    }
    </code>
    J 1 Reply Last reply 29 Mar 2023, 14:08
    0
    • D dauriac
      29 Mar 2023, 13:54

      I would like to create a 2-widgets stacked widget with the following property:
      if the mouse hovers the stacked widget then the current index is set to 0
      else the current index is set to 1
      The following code produce SIGSEGV. Note also that this->setCurrentIndex(1) in the constructor is ineffective.

      <code>
      MyQStackedWidget::MyQStackedWidget(QWidget *parent) :
          QStackedWidget(parent)
      {
          setAttribute(Qt::WA_Hover);
          fprintf(stderr,"Creator Stacked\n");
          this->setCurrentIndex(1);  // THIS HAS NO EFFECT !!
      }
      
      bool MyQStackedWidget::event(QEvent* e)
      {
          switch(e->type())
          {
          case QEvent::HoverEnter:
              hoverEnter(static_cast<QHoverEvent*>(e));
              return true;
          case QEvent::HoverLeave:
              hoverLeave(static_cast<QHoverEvent*>(e));
              return true;
          default:
              break;
          }
          return QWidget::event(e);
      }
      
      void MyQStackedWidget::enterEvent(QEvent* e)
      {
          fprintf(stderr,"enterEvent MyQStackedWidget\n");
      }
      
      void MyQStackedWidget::leaveEvent(QEvent* e)
      {
          fprintf(stderr,"leaveEvent MyQStackedWidget\n");
      }
      
      void MyQStackedWidget::hoverEnter(QHoverEvent* event)
      {
          fprintf(stderr,"hoverEnter MyQStackedWidget\n");
          int i = (this->currentIndex()+1)%2;
          this->setCurrentIndex(i);
      }
      
      void MyQStackedWidget::hoverLeave(QHoverEvent* event)
      {
          fprintf(stderr,"hoverLeave MyQStackedWidget\n");
          int i = (this->currentIndex()+1)%2;
          this->setCurrentIndex(i);
      }
      </code>
      J Offline
      J Offline
      JonB
      wrote on 29 Mar 2023, 14:08 last edited by
      #2

      @dauriac said in Two-widgets stacked widget switching when mouse hovers it.:

      Note also that this->setCurrentIndex(1) in the constructor is ineffective.

      You cannot set the current index in a QStackedWidget to more than the number of widgets you have added to it. In the constructor that will be 0, hence not setCurrentIndex(1). Since you never add any widgets anywhere you won't ever be able to move the index off -1.

      For a SIGSEGV run under debugger and look at stack trace to see where it emanates from.

      D 1 Reply Last reply 29 Mar 2023, 14:19
      0
      • J JonB
        29 Mar 2023, 14:08

        @dauriac said in Two-widgets stacked widget switching when mouse hovers it.:

        Note also that this->setCurrentIndex(1) in the constructor is ineffective.

        You cannot set the current index in a QStackedWidget to more than the number of widgets you have added to it. In the constructor that will be 0, hence not setCurrentIndex(1). Since you never add any widgets anywhere you won't ever be able to move the index off -1.

        For a SIGSEGV run under debugger and look at stack trace to see where it emanates from.

        D Offline
        D Offline
        dauriac
        wrote on 29 Mar 2023, 14:19 last edited by
        #3

        @JonB
        Thank you for your answer !
        Concerning the ineffective setindex it is clear (sorry I shoud have think a little more!).
        Concerning my real problem I add below the code of the setting of the MyQStackedWidget

            MyQStackedWidget* sw = new MyQStackedWidget();
            QLabel* lab = new QLabel("stacked label");
            QTextEdit* textE = new QTextEdit("textEdit");
            sw->addWidget(textE);  // index 0
            sw->addWidget(lab); // index 1
            mainLayout->addWidget(sw,1,1);
        
        J 1 Reply Last reply 29 Mar 2023, 14:34
        0
        • D dauriac
          29 Mar 2023, 14:19

          @JonB
          Thank you for your answer !
          Concerning the ineffective setindex it is clear (sorry I shoud have think a little more!).
          Concerning my real problem I add below the code of the setting of the MyQStackedWidget

              MyQStackedWidget* sw = new MyQStackedWidget();
              QLabel* lab = new QLabel("stacked label");
              QTextEdit* textE = new QTextEdit("textEdit");
              sw->addWidget(textE);  // index 0
              sw->addWidget(lab); // index 1
              mainLayout->addWidget(sw,1,1);
          
          J Offline
          J Offline
          JonB
          wrote on 29 Mar 2023, 14:34 last edited by JonB
          #4

          @dauriac
          Then with the code above, which in itself will work and not crash, what is the problem? You can change the index after they have been added. If it's the crash I said what to do. If it's the index not changing, make sure the QStackedWidget itself is getting those hover etc. events, maybe they only occur on the widgets placed on it, I don't know.

          D 1 Reply Last reply 29 Mar 2023, 16:30
          0
          • J JonB
            29 Mar 2023, 14:34

            @dauriac
            Then with the code above, which in itself will work and not crash, what is the problem? You can change the index after they have been added. If it's the crash I said what to do. If it's the index not changing, make sure the QStackedWidget itself is getting those hover etc. events, maybe they only occur on the widgets placed on it, I don't know.

            D Offline
            D Offline
            dauriac
            wrote on 29 Mar 2023, 16:30 last edited by
            #5

            @JonB
            I understood : this NOT the QStackedWidget which is hovered or not, but it is the selected widget. And my callback function changes the selected widget, so it generates a new event and so on and so forth ... At the end there is a stack overflow. Easy to prevent with an extra bool variable.

            J 1 Reply Last reply 29 Mar 2023, 16:34
            0
            • D dauriac
              29 Mar 2023, 16:30

              @JonB
              I understood : this NOT the QStackedWidget which is hovered or not, but it is the selected widget. And my callback function changes the selected widget, so it generates a new event and so on and so forth ... At the end there is a stack overflow. Easy to prevent with an extra bool variable.

              J Offline
              J Offline
              JonB
              wrote on 29 Mar 2023, 16:34 last edited by
              #6

              @dauriac
              Ah yes :) When you change the widget being hovered over you get a new hover event for the new widget.....

              1 Reply Last reply
              0
              • D dauriac has marked this topic as solved on 29 Mar 2023, 16:47

              1/6

              29 Mar 2023, 13:54

              • Login

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