Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. How to create a button and place it where I want and then run a function when the button is clicked C++ QT

How to create a button and place it where I want and then run a function when the button is clicked C++ QT

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
c++ qtbuttonclickevent
17 Posts 3 Posters 8.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.
  • R RuWex
    28 Sept 2022, 08:14

    @jsulm
    thank,
    I wrote it in this function (if you know from application- example)

    void MainWindow::open()
    //! [7] //! [8]
    {
        QPushButton *m_button;
        m_button = new QPushButton("My Button", this);
        m_button->show();
        m_button = new QPushButton("My Button", mainWind);
        m_button->setGeometry(150, 150,150,150);
        m_button->clicked();
        if (maybeSave()) {
            QString fileName = QFileDialog::getOpenFileName(this);
            if (!fileName.isEmpty())
                loadFile(fileName);
    
            QFile file(fileName);
             if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                 return;
            StartToSendCommand(fileName, textEdit);//Ruth
        }
    
    
    }
    J Offline
    J Offline
    JonB
    wrote on 28 Sept 2022, 08:23 last edited by JonB
    #5

    @RuWex said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:

    I wrote it in this function (if you know from application- example)

    From what example? Please provide a link.
    But also follow what @jsulm is asking/saying.

    R 1 Reply Last reply 28 Sept 2022, 08:50
    0
    • J jsulm
      28 Sept 2022, 08:18

      @RuWex Please also read the rest of my previous reply...
      I also don't understand why you're creating a button inside open(). What do you want to do with this button?

      R Offline
      R Offline
      RuWex
      wrote on 28 Sept 2022, 08:40 last edited by
      #6

      @jsulm I'm new in qt c++ and I dont so know, what can I do instaed of?
      and i did it in open() because I want the button to appear when the option - open is choosen...

      J 1 Reply Last reply 28 Sept 2022, 08:47
      0
      • R RuWex
        28 Sept 2022, 08:40

        @jsulm I'm new in qt c++ and I dont so know, what can I do instaed of?
        and i did it in open() because I want the button to appear when the option - open is choosen...

        J Online
        J Online
        jsulm
        Lifetime Qt Champion
        wrote on 28 Sept 2022, 08:47 last edited by jsulm
        #7

        @RuWex said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:

        what can I do instaed of?

        First: explain CLEARLY what you want to do.
        And please learn Qt basic stuff like signals/slots (read the links I posted).
        "qt c++" - again, there is no such thing. Are you new to Qt, or also to C++?
        If you need a button to pen a dialog then it should look like this:

        MainWindow::MainWindow(...)
        {
            m_button = new QPushButton("My Button", this);
            m_button->show();
            connect(m_button, &QPushButton::clicked, this, &MainWindow::open);
        }
        
        void MainWindow::open()
        {
            if (maybeSave()) {
                QString fileName = QFileDialog::getOpenFileName(this);
                if (!fileName.isEmpty())
                    loadFile(fileName);
            
                QFile file(fileName);
                 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                     return;
                StartToSendCommand(fileName, textEdit);//Ruth
        }
        

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

        R 1 Reply Last reply 28 Sept 2022, 09:01
        0
        • J JonB
          28 Sept 2022, 08:23

          @RuWex said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:

          I wrote it in this function (if you know from application- example)

          From what example? Please provide a link.
          But also follow what @jsulm is asking/saying.

          R Offline
          R Offline
          RuWex
          wrote on 28 Sept 2022, 08:50 last edited by
          #8

          @JonB
          I took from the example that I got inside the software
          image_1.png

          J 1 Reply Last reply 28 Sept 2022, 08:59
          0
          • R RuWex
            28 Sept 2022, 08:50

            @JonB
            I took from the example that I got inside the software
            image_1.png

            J Offline
            J Offline
            JonB
            wrote on 28 Sept 2022, 08:59 last edited by
            #9

            @RuWex
            If you cannot provide a link I can't look at it.

            1 Reply Last reply
            0
            • J jsulm
              28 Sept 2022, 08:47

              @RuWex said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:

              what can I do instaed of?

              First: explain CLEARLY what you want to do.
              And please learn Qt basic stuff like signals/slots (read the links I posted).
              "qt c++" - again, there is no such thing. Are you new to Qt, or also to C++?
              If you need a button to pen a dialog then it should look like this:

              MainWindow::MainWindow(...)
              {
                  m_button = new QPushButton("My Button", this);
                  m_button->show();
                  connect(m_button, &QPushButton::clicked, this, &MainWindow::open);
              }
              
              void MainWindow::open()
              {
                  if (maybeSave()) {
                      QString fileName = QFileDialog::getOpenFileName(this);
                      if (!fileName.isEmpty())
                          loadFile(fileName);
                  
                      QFile file(fileName);
                       if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                           return;
                      StartToSendCommand(fileName, textEdit);//Ruth
              }
              
              R Offline
              R Offline
              RuWex
              wrote on 28 Sept 2022, 09:01 last edited by
              #10

              @jsulm said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:
              thank you!!

              I'll explain a little what I meant, I want to have the option of opening a file, and after opening the file a "start testing" button will appear, clicking on it will activate the function StartToSendCommand() which receives QString fileName, QPlainTextEdit *textEdit - which is a problem according to the option you gave me, it doesn't give in the function connect() to send parameters. Then I want the function to run on the file I opened... Is my question more understandable?

              J 1 Reply Last reply 28 Sept 2022, 09:09
              0
              • R RuWex
                28 Sept 2022, 09:01

                @jsulm said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:
                thank you!!

                I'll explain a little what I meant, I want to have the option of opening a file, and after opening the file a "start testing" button will appear, clicking on it will activate the function StartToSendCommand() which receives QString fileName, QPlainTextEdit *textEdit - which is a problem according to the option you gave me, it doesn't give in the function connect() to send parameters. Then I want the function to run on the file I opened... Is my question more understandable?

                J Online
                J Online
                jsulm
                Lifetime Qt Champion
                wrote on 28 Sept 2022, 09:09 last edited by
                #11

                @RuWex said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:

                connect()

                Connect is a method in QObject - all subclasses including QPlainTextEdit have it.
                Modified version according your description (I don't understand why you're opening file without doing anything with its content, I also don't know what this textEdit is about and why it needs to be parameter, your code is really strange - I suggest you start from scratch step by step):

                void MainWindow::open()
                {
                    if (maybeSave()) {
                        QString fileName = QFileDialog::getOpenFileName(this);
                
                        QPushButton *button = new QPushButton("Start", this);
                        connect(button, &QPushButton::clicked, this, [=]() { sendCommand(fileName, textEdit); });
                }
                
                void MainWindow::sendCommand(QString fileName, QTextEdit textEdit)
                {
                    if (!fileName.isEmpty())
                        loadFile(fileName);
                        
                    QFile file(fileName);
                     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                         return;
                     StartToSendCommand(fileName, textEdit);//Ruth
                }
                

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

                1 Reply Last reply
                1
                • R Offline
                  R Offline
                  RuWex
                  wrote on 28 Sept 2022, 09:14 last edited by RuWex
                  #12

                  I open a file and use its contents!
                  Basically, I open a txt file which is a file consisting of commands and I want that after the file is selected, it will be possible to click on "start testing" - then it will start running line by line from the file and the commands will be sent, so I actually need this whole process.
                  After what I've explained here, is the code understandable, or does it still not look professional (I'd appreciate comments, I'm new...)

                  and- @jsulm- I dont have link its in my software

                  J 1 Reply Last reply 28 Sept 2022, 10:00
                  0
                  • R Offline
                    R Offline
                    RuWex
                    wrote on 28 Sept 2022, 09:27 last edited by
                    #13

                    wow it works!!!
                    Thank you!
                    I would appreciate an answer to my second question :)

                    1 Reply Last reply
                    0
                    • R RuWex
                      28 Sept 2022, 09:14

                      I open a file and use its contents!
                      Basically, I open a txt file which is a file consisting of commands and I want that after the file is selected, it will be possible to click on "start testing" - then it will start running line by line from the file and the commands will be sent, so I actually need this whole process.
                      After what I've explained here, is the code understandable, or does it still not look professional (I'd appreciate comments, I'm new...)

                      and- @jsulm- I dont have link its in my software

                      J Online
                      J Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on 28 Sept 2022, 10:00 last edited by
                      #14

                      @RuWex said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:

                      I open a file and use its contents!

                      Well, not here:

                      void MainWindow::open()
                      {
                          QPushButton *m_button;
                          m_button = new QPushButton("My Button", this);
                          m_button->show();
                          m_button = new QPushButton("My Button", mainWind);
                          m_button->setGeometry(150, 150,150,150);
                          m_button->clicked();
                          if (maybeSave()) {
                              QString fileName = QFileDialog::getOpenFileName(this);
                              if (!fileName.isEmpty())
                                  loadFile(fileName);
                      
                              QFile file(fileName);
                               if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) // You open the file here but DO NOT USE IT
                                   return;
                              StartToSendCommand(fileName, textEdit);//Ruth
                          }
                      

                      What second question do you mean?

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

                      R 1 Reply Last reply 28 Sept 2022, 10:06
                      0
                      • J jsulm
                        28 Sept 2022, 10:00

                        @RuWex said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:

                        I open a file and use its contents!

                        Well, not here:

                        void MainWindow::open()
                        {
                            QPushButton *m_button;
                            m_button = new QPushButton("My Button", this);
                            m_button->show();
                            m_button = new QPushButton("My Button", mainWind);
                            m_button->setGeometry(150, 150,150,150);
                            m_button->clicked();
                            if (maybeSave()) {
                                QString fileName = QFileDialog::getOpenFileName(this);
                                if (!fileName.isEmpty())
                                    loadFile(fileName);
                        
                                QFile file(fileName);
                                 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) // You open the file here but DO NOT USE IT
                                     return;
                                StartToSendCommand(fileName, textEdit);//Ruth
                            }
                        

                        What second question do you mean?

                        R Offline
                        R Offline
                        RuWex
                        wrote on 28 Sept 2022, 10:06 last edited by
                        #15

                        @jsulm

                        I have this code:

                        QPushButton *m_button= new QPushButton ("Start Testing", this) ;
                        QGridLayout *layout = new QGridLayout(this);
                        layout->addWidget(m_button,150,150, 100,140);
                        m_button->show();

                        how can I place the button, for example in the middle of the window? I use it:

                        QGridLayout *layout = new QGridLayout(this);
                        layout->addWidget(m_button,150,150, 100,140);
                        but its not help... and do nothing can anyone help me?

                        thanks!!!

                        J 1 Reply Last reply 28 Sept 2022, 10:09
                        0
                        • R RuWex
                          28 Sept 2022, 10:06

                          @jsulm

                          I have this code:

                          QPushButton *m_button= new QPushButton ("Start Testing", this) ;
                          QGridLayout *layout = new QGridLayout(this);
                          layout->addWidget(m_button,150,150, 100,140);
                          m_button->show();

                          how can I place the button, for example in the middle of the window? I use it:

                          QGridLayout *layout = new QGridLayout(this);
                          layout->addWidget(m_button,150,150, 100,140);
                          but its not help... and do nothing can anyone help me?

                          thanks!!!

                          J Online
                          J Online
                          jsulm
                          Lifetime Qt Champion
                          wrote on 28 Sept 2022, 10:09 last edited by
                          #16

                          @RuWex I answered this question in my first post in this thread: "You should use layouts to position widgets, see https://doc.qt.io/qt-6/layout.html"

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

                          R 1 Reply Last reply 28 Sept 2022, 10:18
                          3
                          • J jsulm
                            28 Sept 2022, 10:09

                            @RuWex I answered this question in my first post in this thread: "You should use layouts to position widgets, see https://doc.qt.io/qt-6/layout.html"

                            R Offline
                            R Offline
                            RuWex
                            wrote on 28 Sept 2022, 10:18 last edited by RuWex
                            #17

                            @jsulm ou! thank you!
                            you had helped me a lot!!

                            1 Reply Last reply
                            1

                            14/17

                            28 Sept 2022, 10:00

                            • Login

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