Radheya
Radheya

Reputation: 821

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.

mainwindow.ui

enter image description here

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

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.

Upvotes: 0

Views: 1677

Answers (1)

dtech
dtech

Reputation: 49289

Qt uses Qt classes (shocking, I know, who would have thought).

As for strings in particular, you can use QString::fromStdString(const std::string &str) to convert an std::string to a QString.

So, assuming you created your UI form with the designer, your code would be something like thins:

ui->yourLineEdit->setText(QString::fromStdString(yourHelloWorld.hello()));

Naturally, hello() would actually have to return an std::string in order for that to work, that is return StoreValue;.

It might be helpful to specify what kind of "values" are you interested in. Qt works just fine with "standard" ints and doubles, and it can work just fine with any C++ type, but for strings in particular, it should not surprise you if Qt APIs are designed to work with QString.

Upvotes: 5

Related Questions