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. Button press counter

Button press counter

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 6.3k 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.
  • M Offline
    M Offline
    mohamaddanesh44
    wrote on last edited by
    #1

    hi
    i have a button and i want to count how many times it pressed.

    thanks

    1 Reply Last reply
    0
    • clogwogC Offline
      clogwogC Offline
      clogwog
      wrote on last edited by
      #2

      i see in your previous posts that you know how to use signals and slots, so what seems to be the problem ?

      an integer in your dialog should be able to incremented

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mohamaddanesh44
        wrote on last edited by
        #3

        yeah i think about it but it has a problem
        i write this code in my QPushButton slot

        @
        for(int i=0;i<20;i++)
        {
        if(i%2==0){
        italic->setStyleSheet("QPushButton{color: gray ;}");
        }
        else
        {
        italic->setStyleSheet("QPushButton{color:white;}");
        }
        }
        @

        but it works wrong
        could you help me?

        1 Reply Last reply
        0
        • clogwogC Offline
          clogwogC Offline
          clogwog
          wrote on last edited by
          #4

          i doubt i can help you, unless you describe what you are trying to do.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mohamaddanesh44
            wrote on last edited by
            #5

            i have a QTextEdit and some QPushButtons like italic and bold...
            for the first time when i click on my buttons i want their style sheet change but for 2nd time click i want the old style sheet
            something like an office word's bold button

            1 Reply Last reply
            0
            • clogwogC Offline
              clogwogC Offline
              clogwog
              wrote on last edited by
              #6

              sounds like you want a toggle button

              http://qt-project.org/doc/qt-5.1/qtwidgets/qabstractbutton.html#checkable-prop

              so after you create the button:
              @italic->setCheckable(true);@
              and then in your slot:

              @if( italic->isChecked() )
              italic->setStyleSheet("QPushButton{color: gray ;}");
              else
              italic->setStyleSheet("QPushButton{color:white;}");@

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mohamaddanesh44
                wrote on last edited by
                #7

                be careful i have QPushButton i don't want to have a check button

                1 Reply Last reply
                0
                • clogwogC Offline
                  clogwogC Offline
                  clogwog
                  wrote on last edited by
                  #8

                  i'm sorry , that is what i read from your description.

                  if you are looking for a word like 'bold' and 'italic' button, then inside your edit field you need to find out if the current 'word' your cursor is on is already 'bold' and if it is then enable/disable your QPushbuttons accordingly.

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    eMixam
                    wrote on last edited by
                    #9

                    [quote author="mohamaddanesh44" date="1378706736"]
                    @
                    for(int i=0;i<20;i++)
                    {
                    if(i%2==0){
                    italic->setStyleSheet("QPushButton{color: gray ;}");
                    }
                    else
                    {
                    italic->setStyleSheet("QPushButton{color:white;}");
                    }
                    }
                    @
                    [/quote]

                    If this is in the clicked() slot it won't work.

                    You're trying to change the color depending on the int i but it's range is in the loop only so the result will be the same each time you click on a button.
                    You need to set it static or as a class variable and increment it each time you click the button.

                    @void QAbstractButton::clicked()
                    {
                    static int i = 0;
                    i++;

                    /* your code goes here */
                    }@

                    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