如何从Qt中的标准C++函数中获取值

Dhr*_*ify 0 c++ qt

这可能听起来没有问题,但我是Qt的新手,并且在使用C++进行编程方面有一些经验.我在最近两天努力奋斗只是为了在QT中制作简单的gui来从现有的C++程序中获取值.

我在下面给出了示例ui,并且当用户按下按钮时我想在文本字段中存储来自c ++文件的字符串值.

mainwindow.ui

在此输入图像描述

应该从存​​储在StoreValue字符串变量中的c ++程序下面获取应存储在空文本框中的值:

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

};
Run Code Online (Sandbox Code Playgroud)

目前在Qt我的项目的源文件夹中我添加了这个额外的hello.cpp文件以及默认的mainwindow.cpp.还存在mainwindow.ui文件,它只是上面提到的GUI中组成的组件的xml结构.

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;
}
Run Code Online (Sandbox Code Playgroud)

最后我的

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>
Run Code Online (Sandbox Code Playgroud)

我想要的是:

由于我的主要目标是在后端编写c ++代码并在前端使用Qt生成ui,我只想使用从标准C++代码返回的值和函数作为GUI的输入.

所以在上面的例子中,当用户按下按钮时,我想将从StoreValuec ++函数中的字符串中取出的值打印void hello()到文本框中.

我尝试了什么:

从这两天开始,我只是阅读和理解框架.我还关注了以下一些有用的链接:

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

是否可以让现有的C++代码与Qt一起使用?

和许多其他谷歌和Youtube搜索,但我根本无法连接正确的点.此时我已经筋疲力尽,我只是在寻找一个简单的例子,其中使用Qt在GUI中提取标准c ++文件中的值和方法.

PS

这可以通过使用内置Qt框架的几个语法命令轻松解决,但我愿意存储来自现有标准C++文件中编写的命令的值.

之后很好,如果我必须使用框架语法使这个值可用于整个GUI应用程序.

非常感谢你的努力.

dte*_*ech 5

Qt使用Qt课程(令人震惊,我知道,谁会想到).

至于字符串,您可以使用QString::fromStdString(const std::string &str)std::stringa 转换为a QString.

因此,假设您使用设计器创建了UI表单,您的代码将类似于:

ui->yourLineEdit->setText(QString::fromStdString(yourHelloWorld.hello()));
Run Code Online (Sandbox Code Playgroud)

当然,hello()实际上必须返回一个std::string才能使其工作,即return StoreValue;.

指定你感兴趣的"值"可能会有所帮助.Qt可以很好地使用"标准"整数和双精度,并且它可以适用于任何C++类型,但对于字符串尤其如此,它不应该令人惊讶如果Qt API可以使用的话QString.