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. [solved] Modify form through main.cpp

[solved] Modify form through main.cpp

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 1.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.
  • T Offline
    T Offline
    Twentyone
    wrote on 11 Jul 2012, 21:01 last edited by
    #1

    hi,
    i'm sorry if my english is not good enough but i'm italian.

    I just started learning -QT- Qt and so, when i opened Qt Creator for the first time, i created a new -QT- Qt Gui Application.
    I added in the form a QPushButton, and if i build the program it runs correctly, but i don't understand how to call the QPushButton (whose name is Btest) in the main.cpp.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on 12 Jul 2012, 05:10 last edited by
      #2

      Hi,

      Welcome to the forum,

      For your question , its not a good idea to access the MainWindow(form) components in main.cpp as you can easily access ui components in your mainwindow class and do the required operations.

      But in order to access ui components in main.cpp you can try something like.

      main.cpp

      @int main(int argc, char *argv[])
      {
      QApplication app(argc, argv);

      MainWindow window;
      QList<QObject*> objectList;
      objectList = window.children();   //returns a list of child objects.
      
      foreach (QObject *obj, objectList) {
          qDebug()<<obj->objectName();
          if(obj->objectName()==QString("Btest"))
          {
              QPushButton *btn = static_cast<QPushButton*>(obj);
              //do something
          }
      }
      
      window.show();
      
      return app.exec&#40;&#41;;
      

      }@

      Other way is :

      @#include "ui_MainWindow.h"

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      Ui::MainWindow window;
      window.Btest->setText("Hello World");

      //do something.......
      return a.exec();
      }@

      But I still recommend to keep the main.cpp clean.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on 12 Jul 2012, 06:32 last edited by
        #3

        I'm not quite sure what you mean by call, but

        • if you want to get a pointer to the button in <code>MainWindow</code> use <code>ui->Btest</code>
        • if you want to get a pointer to the button in <code>main</code> use <code>mainWindow.findChild<QPushButton*>("Btest")</code>
        • if you want to react on the push button beeing clicked either use <code>connect(...)</code> or create a slot called <code>on_Btest_clicked()</code>
        1 Reply Last reply
        0
        • T Offline
          T Offline
          Twentyone
          wrote on 12 Jul 2012, 07:39 last edited by
          #4

          Thank you, it is exactly what i was looking for :D

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mlong
            wrote on 12 Jul 2012, 13:32 last edited by
            #5

            Glad you got the answer you needed. Please be sure and edit the original post and add [Solved] to the title! Thanks!

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply
            0

            1/5

            11 Jul 2012, 21:01

            • Login

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