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. How to get value from standard C++ function in Qt
QtWS25 Last Chance

How to get value from standard C++ function in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
qt5c++gui
8 Posts 2 Posters 6.6k 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.
  • ShawnyS Offline
    ShawnyS Offline
    Shawny
    wrote on last edited by
    #1

    This may sound noob question but I am new to Qt and have some experience in programming with C++. I am struggling very hard from last two days just to make simple gui in QT to fetch values from existing C++ program.

    I made below given sample ui and I want to store string value from c++ file in text field when user presses button.

    Click to View GUI

    The value that should be stored in the empty text box should be fetched from below c++ program stored in StoreValue string variable:

    hello.cpp:

    #include <string>
    #include <iostream>
    
    class helloWorld
    {
    public:
    
        std::string hello(){
    
            std::string StoreValue = "Hello World!"; // print this value in text box
            return StoreValue;
        }
    
    };
    

    currently in source folder of my project in Qt I have added this additional hello.cpp file along with default mainwindow.cpp. There is also mainwindow.ui file present which is just a xml structure of components laid out in above mentioned GUI.

    mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    and finally my

    mainwindow.ui:

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>321</width>
        <height>117</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralWidget">
       <widget class="QPushButton" name="pushButton">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>10</y>
          <width>85</width>
          <height>27</height>
         </rect>
        </property>
        <property name="text">
         <string>Print Hello</string>
        </property>
       </widget>
       <widget class="QTextBrowser" name="textBrowser">
        <property name="geometry">
         <rect>
          <x>120</x>
          <y>10</y>
          <width>181</width>
          <height>31</height>
         </rect>
        </property>
       </widget>
      </widget>
      <widget class="QToolBar" name="mainToolBar">
       <attribute name="toolBarArea">
        <enum>TopToolBarArea</enum>
       </attribute>
       <attribute name="toolBarBreak">
        <bool>false</bool>
       </attribute>
      </widget>
      <widget class="QStatusBar" name="statusBar"/>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>321</width>
         <height>22</height>
        </rect>
       </property>
       <widget class="QMenu" name="menuFile">
        <property name="title">
         <string>File</string>
        </property>
       </widget>
       <addaction name="menuFile"/>
      </widget>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    

    What I want:

    Since my main aim is to write c++ code at back end and make ui using Qt at front end, I only want to use values and functions returning from standard C++ codes as an input for my GUI.

    So in above case I want to print value fetched from StoreValue string in c++ function void hello() into text box when user presses button.

    What I tried:

    From these two days I am simply reading and understanding the framework. I also followed some of the useful links below:

    http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html

    http://stackoverflow.com/questions/16082803/is-it-possible-to-have-existing-c-code-work-with-qt

    and many other Google and Youtube searches but I am simply not able to connect the right dots. At this point i am completely exhausted and I am just looking for one simple example where values and methods from standard c++ file is extracted in GUI using Qt.

    P.S.

    This may get easily solved by using several syntactical commands of inbuilt Qt Framework but I am willing to store values from commands written in existing standard C++ file.

    Afterwards its fine if i have to use framework syntax to make this values available to entire GUI application.

    Thank you very much for your efforts.

    ? 1 Reply Last reply
    0
    • ShawnyS Shawny

      This may sound noob question but I am new to Qt and have some experience in programming with C++. I am struggling very hard from last two days just to make simple gui in QT to fetch values from existing C++ program.

      I made below given sample ui and I want to store string value from c++ file in text field when user presses button.

      Click to View GUI

      The value that should be stored in the empty text box should be fetched from below c++ program stored in StoreValue string variable:

      hello.cpp:

      #include <string>
      #include <iostream>
      
      class helloWorld
      {
      public:
      
          std::string hello(){
      
              std::string StoreValue = "Hello World!"; // print this value in text box
              return StoreValue;
          }
      
      };
      

      currently in source folder of my project in Qt I have added this additional hello.cpp file along with default mainwindow.cpp. There is also mainwindow.ui file present which is just a xml structure of components laid out in above mentioned GUI.

      mainwindow.cpp:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      

      and finally my

      mainwindow.ui:

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>MainWindow</class>
       <widget class="QMainWindow" name="MainWindow">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>321</width>
          <height>117</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>MainWindow</string>
        </property>
        <widget class="QWidget" name="centralWidget">
         <widget class="QPushButton" name="pushButton">
          <property name="geometry">
           <rect>
            <x>20</x>
            <y>10</y>
            <width>85</width>
            <height>27</height>
           </rect>
          </property>
          <property name="text">
           <string>Print Hello</string>
          </property>
         </widget>
         <widget class="QTextBrowser" name="textBrowser">
          <property name="geometry">
           <rect>
            <x>120</x>
            <y>10</y>
            <width>181</width>
            <height>31</height>
           </rect>
          </property>
         </widget>
        </widget>
        <widget class="QToolBar" name="mainToolBar">
         <attribute name="toolBarArea">
          <enum>TopToolBarArea</enum>
         </attribute>
         <attribute name="toolBarBreak">
          <bool>false</bool>
         </attribute>
        </widget>
        <widget class="QStatusBar" name="statusBar"/>
        <widget class="QMenuBar" name="menuBar">
         <property name="geometry">
          <rect>
           <x>0</x>
           <y>0</y>
           <width>321</width>
           <height>22</height>
          </rect>
         </property>
         <widget class="QMenu" name="menuFile">
          <property name="title">
           <string>File</string>
          </property>
         </widget>
         <addaction name="menuFile"/>
        </widget>
       </widget>
       <layoutdefault spacing="6" margin="11"/>
       <resources/>
       <connections/>
      </ui>
      

      What I want:

      Since my main aim is to write c++ code at back end and make ui using Qt at front end, I only want to use values and functions returning from standard C++ codes as an input for my GUI.

      So in above case I want to print value fetched from StoreValue string in c++ function void hello() into text box when user presses button.

      What I tried:

      From these two days I am simply reading and understanding the framework. I also followed some of the useful links below:

      http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html

      http://stackoverflow.com/questions/16082803/is-it-possible-to-have-existing-c-code-work-with-qt

      and many other Google and Youtube searches but I am simply not able to connect the right dots. At this point i am completely exhausted and I am just looking for one simple example where values and methods from standard c++ file is extracted in GUI using Qt.

      P.S.

      This may get easily solved by using several syntactical commands of inbuilt Qt Framework but I am willing to store values from commands written in existing standard C++ file.

      Afterwards its fine if i have to use framework syntax to make this values available to entire GUI application.

      Thank you very much for your efforts.

      ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      @Shawny Hi, and welcome to the Qt forum!

      Don't worry, it's really easy once you know how. Look:

      // This is your function
      std::string sayHi()
      {
          const std::string s = "Hi!";
          return s;
      }
      
      // This is the implementation of your on_pushButton_clicked() slot
      void MainWindow::on_pushButton_clicked()
      {
          const QString s = QString::fromStdString( sayHi() ); // convert std::string to QString
          ui->lineEdit->setText( s ); // show text in label
      }
      
      1 Reply Last reply
      1
      • ShawnyS Offline
        ShawnyS Offline
        Shawny
        wrote on last edited by Shawny
        #3

        ohh i get it now. Thank you very much for your feedback. I just have two more questions now.

        1). Can you tell me how to put info of my hello.cpp in header so that in my main file can also import values from functions and classes of other cpp files kept in same source folder?

        2). How to accomplish your solution by slot and signal method using syntax connect(sender,this,receiver,this)? I am reading documents on it but not able to figure out exactly how it can happen.

        Sorry for these basic questions but its little difficult to understand this framework at first. I am totally new to Qt framework. Though currently I am reading lots of documents.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Regarding your first question: You can split your helloWorld class into a header and source file like this:

          hello.h

          #include <string>
          
          class helloWorld
          {
          public:
              std::string hello(); 
          };
          

          hello.cpp

          #include "hello.h"
          
          std::string helloWorld::hello() { 
            std::string StoreValue = "Hello World!"; 
            return StoreValue;
          }
          
          1 Reply Last reply
          2
          • ShawnyS Offline
            ShawnyS Offline
            Shawny
            wrote on last edited by
            #5

            @Wieland Thank you very much. How can I mark this thread solved?

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Regarding your second question: You don't have to explicitly connect anything here. Just do the following in Qt Creator:

              Right-click on the button...
              0
              "Go to slot..."
              1
              "clicked()"
              2
              OK

              This will create all necessary function stubs.

              1 Reply Last reply
              2
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                This thread explains how to mark your thread as solved: http://forum.qt.io/topic/62700/hitchhiker-s-visual-guide-to-the-qt-forum
                :-)

                1 Reply Last reply
                0
                • ShawnyS Offline
                  ShawnyS Offline
                  Shawny
                  wrote on last edited by
                  #8

                  Thank you for your depth explanations. I am able to understand a lot of things now. Its time to tweak around and learn as I go further :)

                  1 Reply Last reply
                  0

                  • Login

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