qt调用java库_从Qt中的类调用插槽

这篇博客主要讲述了在Qt应用中遇到的关于从窗口类调用后台类的槽函数问题。作者在尝试从服务器获取文件时,遇到了连接函数调用错误。通过更新代码并避免循环包含解决了问题。示例代码包括了mainwindow、Backend类的实现,以及主函数main.cpp的部分内容。
摘要由CSDN通过智能技术生成

我正在尝试编写一个从服务器获取文件的应用程序 .

我有一个'Window'类(mainwindow.cpp,它是一个widget类,它是UI),然后我有一个'Backend'类(Backend.cpp) .

GUI有一个按钮和两个单选按钮 . 如果选择单选按钮“远程”,则单击按钮将导致从服务器获取文件 .

但是,在Backend.cpp的'connect'调用中存在一些问题,我无法弄清楚 . 我得到的错误是:没有匹配函数调用'QObject :: connect(QNetworkReply *&),const char [13],Backend * const,const char [20])'

以下是代码:

ANSWER: Avoid circular inclusions!!!! Here are the updated codes:

mainwindow.h

#ifndef WINDOW_H

#define WINDOW_H

#include

#include

#include

#include

#include

#include

#include "Backend.h"

class QGroupBox;

class Window : public QWidget

{

Q_OBJECT

public:

Window(QWidget *parent = 0);

QTcpSocket *conn;

QFile *file;

QUrl url;

Backend backend_inst;

private:

QRadioButton *button_local;

QRadioButton *button_remote;

QGroupBox *createPushButtonGroup();

private slots:

void onClick_button1();

void onCheck_local();

void onCheck_remote();

};

#endif

mainwindow.c

#include

#include "mainwindow.h"

Window::Window(QWidget *parent)

: QWidget(parent)

{

QGridLayout *grid = new QGridLayout;

grid->addWidget(createPushButtonGroup(), 1, 1);

setLayout(grid);

setWindowTitle(tr("File-Fetch App"));

resize(480, 420);

}

QGroupBox *Window::createPushButtonGroup()

{

QGroupBox *groupBox = new QGroupBox();

QPushButton *pushButton1 = new QPushButton(tr("Fetch Files!!"));

button_local = new QRadioButton(tr("&Download Files from Local Storage"));

button_remote = new QRadioButton(tr("&Download Files from a Web-Server"));

button_local->setChecked(1);

connect(pushButton1,SIGNAL(clicked()), this, SLOT(onClick_button1()));

QVBoxLayout *vbox = new QVBoxLayout;

vbox->addWidget(pushButton1);

vbox->addSpacing(50);

vbox->addWidget(button_local);

vbox->addWidget(button_remote);

vbox->addStretch(1);

groupBox->setLayout(vbox);

return groupBox;

}

void Window::onClick_button1()

{

QTextStream out(stdout);

out << "fetch button clicked\n";

if (button_local->isChecked()){

onCheck_local();

}

else if (button_remote->isChecked()){

onCheck_remote();

}

}

void Window::onCheck_local()

{

QTextStream out(stdout);

out << "local update button checked\n";

}

void Window::onCheck_remote()

{

QTextStream out(stdout);

out << "remote update button checked\n";

QString pathname= "http://192.168.1.1:8000/example.txt";

QUrl webaddr = pathname;

backend_inst.FetchFile(webaddr);

}

Backend.h

#ifndef BACKEND_H

#define BACKEND_H

#include

#include

#include

#include

#include

class Backend : public QObject

{

Q_OBJECT

public:

Backend(QObject* parent=0);

void FetchFile(QUrl fpath);

public slots:

void getBytesFromFile();

private:

QNetworkReply *reply;

QNetworkAccessManager qnam;

};

#endif // BACKEND_H

Backend.cpp

#include "Backend.h"

Backend::Backend(QObject* parent)

: QObject(parent)

{

}

void Backend::FetchFile(QUrl fpath)

{

reply = qnam.get(QNetworkRequest(fpath));

QObject::connect(reply, SIGNAL(readyRead()),this, SLOT(getBytesFromFile()));

//qnam = new QNetworkAccessManager;

//QObject::connect(&qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(getBytesFromFile()));

}

void Backend::getBytesFromFile(){

QByteArray downloadedData;

QTextStream out(stdout);

out << "we are loading data from URL\n";

downloadedData =reply->readAll();

out << downloadedData;

delete reply;

}

main.cpp中

#include

#include "mainwindow.h"

int main(int argc, char *argv[])

{

QApplication app(argc, argv);

Window window;

window.show();

return app.exec();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值