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. Testing a button click event in QtTest
Forum Updated to NodeBB v4.3 + New Features

Testing a button click event in QtTest

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.4k 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.
  • S Offline
    S Offline
    shravan_121
    wrote on last edited by
    #1

    How to test a button click event of the button created like :
    Button *plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));
    in widget based app.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      As far as I know createButton is not a standard method of Qt. In any case, what do you mean by Testing? if it's unit testing you can use https://doc.qt.io/qt-5/qttestlib-tutorial3-example.html

      QTest::mouseClick(plusButton,Qt::LeftButton);

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      4
      • S Offline
        S Offline
        shravan_121
        wrote on last edited by
        #3

        This is for unit Testing :
        Calculator::Calculator(QWidget *parent)
        : QWidget(parent)
        {
        Button *plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));
        }
        This is the code snippet and I tried
        QTest::mouseClick(calculator->plusButton,Qt::LeftButton);
        But it shows nomember named plusButton in calculator but in the calculator pointer suggestions additiveOperatorClicked() is shown.

        jsulmJ 1 Reply Last reply
        0
        • S shravan_121

          This is for unit Testing :
          Calculator::Calculator(QWidget *parent)
          : QWidget(parent)
          {
          Button *plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));
          }
          This is the code snippet and I tried
          QTest::mouseClick(calculator->plusButton,Qt::LeftButton);
          But it shows nomember named plusButton in calculator but in the calculator pointer suggestions additiveOperatorClicked() is shown.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @shravan_121 said in Testing a button click event in QtTest:

          But it shows nomember named plusButton in calculator

          Because your plusButton is a LOCAL variable inside constructor. Add it as member variable to Calculator class.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply
          2
          • S Offline
            S Offline
            shravan_121
            wrote on last edited by
            #5

            @jsulm Thank You for helping me resolve the problem

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @shravan_121 said in Testing a button click event in QtTest:

              But it shows nomember named plusButton in calculator

              Because your plusButton is a LOCAL variable inside constructor. Add it as member variable to Calculator class.

              S Offline
              S Offline
              shravan_121
              wrote on last edited by
              #6

              @jsulm can you say me how to do that

              jsulmJ 1 Reply Last reply
              0
              • S shravan_121

                @jsulm can you say me how to do that

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @shravan_121 This is really basic C++, I suggest you read a book.

                class Calculator
                {
                public:
                    Button *plusButton;
                };
                ...
                Calculator::Calculator(QWidget *parent)
                : QWidget(parent)
                {
                    plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));
                }
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                S 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @shravan_121 This is really basic C++, I suggest you read a book.

                  class Calculator
                  {
                  public:
                      Button *plusButton;
                  };
                  ...
                  Calculator::Calculator(QWidget *parent)
                  : QWidget(parent)
                  {
                      plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));
                  }
                  
                  S Offline
                  S Offline
                  shravan_121
                  wrote on last edited by
                  #8

                  @jsulm The problem is solved and thanks for your support and having patience to solve the problem.

                  1 Reply Last reply
                  1

                  • Login

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