Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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.



  • @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
    }
    


  • 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.



  • 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;
    }
    


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



  • 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.



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



  • 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 :)


Log in to reply