Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从c函数在qtextedit上显示消息_C++_C_Qt_Api - Fatal编程技术网

C++ 从c函数在qtextedit上显示消息

C++ 从c函数在qtextedit上显示消息,c++,c,qt,api,C++,C,Qt,Api,我试图通过c函数在qtextedit上显示一条消息。 我使用一个api类从c函数调用qthello方法。 qthello方法有两个位置。 qDebug语句正确执行并显示“hellooo”。但是textEdit->setText(“你好”)表达不起作用。 怎么了 project.pro QT += widgets TEMPLATE += app SOURCES += \ main.cpp \ mainwindow.cpp \ hello.c \ api.cpp HEADERS

我试图通过c函数在qtextedit上显示一条消息。 我使用一个api类从c函数调用qthello方法。 qthello方法有两个位置。 qDebug语句正确执行并显示“hellooo”。但是textEdit->setText(“你好”)表达不起作用。 怎么了

project.pro

QT        += widgets
TEMPLATE  += app
SOURCES   += \
main.cpp \
mainwindow.cpp \
hello.c \
api.cpp
HEADERS += \
mainwindow.h \
hello.h \
api.h
main.cpp

#include <QApplication>
#include "mainwindow.h"
#include "hello.h"
int main(int argc, char *argv[]){
QApplication a(argc, argv);
MainWindow w;
hello();  //c function
w.show();
return a.exec();
}
#include "mainwindow.h"
int main(int argc, char *argv[]){
QApplication a(argc, argv);
//MainWindow w;
//w.makeWindow();
//w.show();
hello();
return a.exec();
}
你好,c

#include "hello.h"
void hello(void){
char * s = "hellooooooooo";
//write(1, s, strlen(s));

void* myclass = MyClass_create();
MyClass_qthello(myclass , s);
MyClass_release(myclass);
}
#include "hello.h"
void hello(void){
char * s = "hellllllllllllllllllloooo\n";
MyClass_create(s);
}
api.h

api.cpp

#include "api.h"
#include "mainwindow.h"
void * MyClass_create() {
return new MainWindow;
}
void MyClass_release(void* myclass) {
delete static_cast<MainWindow*>(myclass);
}
void MyClass_qthello(void* myclass , char * s) {
static_cast<MainWindow*>(myclass)->qthello(s);
}
#include "api.h"
#include "mainwindow.h"
void * MyClass_create(char * s) {
MainWindow * a = new MainWindow;
a->show();
a->qthello(s);
}
#包括“api.h”
#包括“mainwindow.h”
void*MyClass_create(){
返回新的主窗口;
}
作废MyClass_发布(作废*MyClass){
删除静态类型转换(myclass);
}
void MyClass_qtallo(void*MyClass,char*s){
静态_cast(myclass)->qthello(s);
}
主窗口

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTextEdit>
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
 void qthello(char * s);
private:
QTextEdit * textEdit;
};
#endif
\ifndef主窗口
#定义主窗口
#包括
#包括
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=0);
~main窗口();
无效qthello(字符*s);
私人:
QTextEdit*文本编辑;
};
#恩迪夫
mainwindow.cpp

#include "mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget * parent):
QMainWindow(parent)
{
textEdit = new QTextEdit(this);
this->setCentralWidget(textEdit);
}

MainWindow::~MainWindow(){}

void MainWindow::qthello(cahr * s)
{
QString  str=s;
qDebug()<<str; //worked
textEdit->setText(str); // did not work
}
#包括“mainwindow.h”
#包括
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父窗口)
{
text编辑=新的QTextEdit(此);
此->设置中心Widget(文本编辑);
}
MainWindow::~MainWindow(){}
void主窗口::qthello(cahr*s)
{
QString str=s;

qDebug()我发现了问题。我使用了两个不同的对象(MainWindow对象)

main.cpp

#include <QApplication>
#include "mainwindow.h"
#include "hello.h"
int main(int argc, char *argv[]){
QApplication a(argc, argv);
MainWindow w;
hello();  //c function
w.show();
return a.exec();
}
#include "mainwindow.h"
int main(int argc, char *argv[]){
QApplication a(argc, argv);
//MainWindow w;
//w.makeWindow();
//w.show();
hello();
return a.exec();
}
你好,c

#include "hello.h"
void hello(void){
char * s = "hellooooooooo";
//write(1, s, strlen(s));

void* myclass = MyClass_create();
MyClass_qthello(myclass , s);
MyClass_release(myclass);
}
#include "hello.h"
void hello(void){
char * s = "hellllllllllllllllllloooo\n";
MyClass_create(s);
}
api.cpp

#include "api.h"
#include "mainwindow.h"
void * MyClass_create() {
return new MainWindow;
}
void MyClass_release(void* myclass) {
delete static_cast<MainWindow*>(myclass);
}
void MyClass_qthello(void* myclass , char * s) {
static_cast<MainWindow*>(myclass)->qthello(s);
}
#include "api.h"
#include "mainwindow.h"
void * MyClass_create(char * s) {
MainWindow * a = new MainWindow;
a->show();
a->qthello(s);
}

现在,它开始工作。

cahr是如何进入你的MCVE的?同样,对于MCVE,你应该把所有的东西都放在一个
.cpp
文件中,包括模块范围的头文件(这里:
#include
),放弃包括警卫(毕竟是
.cpp
文件),等等。我没有得到任何错误。我想知道为什么函数qthello中的第一条语句被执行,但第二条语句不起作用?上面的第一条注释指出“cahr”不可能编译,所以这里的代码实际上不是真实的,这让人怀疑我们是否看到了与您使用的相同的东西。除了尝试这样做的极度复杂的混乱之外,我没有发现textEdit->setText(str)有任何错误调用。此调用前后textEdit显示了什么?有任何更改吗?什么是“cahr”意思?这是我所有的代码。当我从MainWindow构造函数调用qthello方法时,textEdit显示了消息,但当我从c函数调用它时,qdebug工作,textEdit没有显示消息。