Button press counter



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

    thanks



  • 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



  • 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?



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



  • 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



  • 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;}");@



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



  • 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.



  • [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 */
    }@


Log in to reply
 

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