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
Forum Updated to NodeBB v4.3 + New Features

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.7k 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.
  • JonBJ JonB

    @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 last edited by
    #8

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

    JonBJ 1 Reply Last reply
    0
    • R RuWex

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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #9

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

      1 Reply Last reply
      0
      • jsulmJ jsulm

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

        jsulmJ 1 Reply Last reply
        0
        • R RuWex

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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 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

            jsulmJ 1 Reply Last reply
            0
            • R Offline
              R Offline
              RuWex
              wrote on 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

                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

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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
                0
                • jsulmJ jsulm

                  @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 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!!!

                  jsulmJ 1 Reply Last reply
                  0
                  • R RuWex

                    @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!!!

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 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
                    3
                    • jsulmJ jsulm

                      @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 last edited by RuWex
                      #17

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

                      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