我想从QT中的main.cpp中的c ++文件中获取函数的结果。
例
.h文件:
ifndef HOTPLUGWATCHER_GUI_H
#define HOTPLUGWATCHER_GUI_H
#include <QWidget>
class QTextBrowser;
class QSystemTrayIcon;
class QLabel;
class QPushButton;
class QToolButton;
class QDeviceWatcher;
class HotplugWatcher_GUI : public QWidget
{
Q_OBJECT
public:
explicit HotplugWatcher_GUI(QWidget *parent = 0);
~HotplugWatcher_GUI();
void appendMessage(const QString& msg);
public slots:
void toggleWatch();
void showDetail(bool show);
void slotDeviceAdded(const QString& dev);
void slotDeviceRemoved(const QString& dev);
void slotDeviceChanged(const QString& dev);
private:
QDeviceWatcher *watcher;
QLabel *state;
QTextBrowser *msg_view;
QPushButton *button;
QToolButton *detail_button;
QSystemTrayIcon *tray;
};
#endif // HOTPLUGWATCHER_GUI_H
cpp文件:
#include "hotplugwatcher_gui.h"
#include <QtCore/QThread>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTextBrowser>
#include <QToolButton>
#include <QSystemTrayIcon>
#include <QMessageBox>
#include "qdevicewatcher.h"
HotplugWatcher_GUI::HotplugWatcher_GUI(QWidget *parent) :
QWidget(parent)
{
QVBoxLayout *vbl = new QVBoxLayout(this);
setLayout(vbl);
state = new QLabel(this);
//qDebug()<<state;
button = new QPushButton(tr("Start"), this);
detail_button = new QToolButton(this);
detail_button->setText(tr("Detail"));
detail_button->setCheckable(true);
detail_button->setChecked(true);
msg_view = new QTextBrowser(this);
msg_view->setLineWrapMode(QTextEdit::NoWrap);
QHBoxLayout *hbl = new QHBoxLayout(this);
hbl->addWidget(button);
hbl->addWidget(detail_button);
vbl->addWidget(state);
vbl->addLayout(hbl);
vbl->addWidget(msg_view);
tray = new QSystemTrayIcon(this);
tray->show();
watcher = new QDeviceWatcher;
watcher->appendEventReceiver(this);
connect(watcher, SIGNAL(deviceAdded(QString)), this, SLOT(slotDeviceAdded(QString)), Qt::DirectConnection);
connect(watcher, SIGNAL(deviceChanged(QString)), this, SLOT(slotDeviceChanged(QString)), Qt::DirectConnection);
connect(watcher, SIGNAL(deviceRemoved(QString)), this, SLOT(slotDeviceRemoved(QString)), Qt::DirectConnection);
connect(button, SIGNAL(clicked()), SLOT(toggleWatch()));
connect(detail_button, SIGNAL(toggled(bool)), SLOT(showDetail(bool)));
}
HotplugWatcher_GUI::~HotplugWatcher_GUI()
{
}
void HotplugWatcher_GUI::appendMessage(const QString &msg)
{
msg_view->append(msg);
}
void HotplugWatcher_GUI::toggleWatch()
{
if (watcher->isRunning()) {
if (!watcher->stop()) {
QMessageBox::warning(0, tr("WARNGING"), tr("Failed to stop"));
}
button->setText(tr("Start"));
} else {
if (!watcher->start()) {
QMessageBox::warning(0, tr("WARNGING"), tr("Failed to start"));
}
button->setText(tr("Stop"));
}
}
void HotplugWatcher_GUI::showDetail(bool show)
{
msg_view->setVisible(show);
int w = width();
adjustSize(); //width changes
resize(w, height());
}
void HotplugWatcher_GUI::slotDeviceAdded(const QString &dev)
{
qDebug("tid=%#x: add %s", (quintptr) QThread::currentThreadId(), qPrintable(dev));
state->setText("<font color=#0000ff>Add: </font>" + dev);
tray->showMessage(tr("New device"), dev);
}
void HotplugWatcher_GUI::slotDeviceChanged(const QString &dev)
{
qDebug("tid=%#x: change %s", (quintptr) QThread::currentThreadId(), qPrintable(dev));
state->setText("<font color=#0000ff>Change: </font>" + dev);
tray->showMessage(tr("Change device"), dev);
}
void HotplugWatcher_GUI::slotDeviceRemoved(const QString &dev)
{
qDebug("tid=%#x: remove %s", (quintptr) QThread::currentThreadId(), qPrintable(dev));
state->setText("<font color=#0000ff>Remove: </font>" + dev);
tray->showMessage(tr("Remove device"), dev);
}
和main.cpp
#include <QApplication>
#include "hotplugwatcher_gui.h"
static HotplugWatcher_GUI *gui = 0;
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
#define qInstallMessageHandler qInstallMsgHandler
void MsgOuput(QtMsgType type, const char *msg)
{
#else
void MsgOuput(QtMsgType type, const QMessageLogContext &, const QString& msg)
{
#endif
Q_UNUSED(type)
if (gui)
gui->appendMessage(msg);
}
int main(int argc, char *argv[])
{
qInstallMessageHandler(MsgOuput);
QApplication a(argc, argv);
HotplugWatcher_GUI hotplug;
gui = &hotplug;
hotplug.show();
return a.exec();
}
所以这个打开小部件有按钮,当按下“星”按钮时,我可以在小部件上看到一些信息,当我连接usb设备时。
这是开始的好点。
但是,我不想拥有小部件,而是将收到的数据发送到QML来使用。
首先是主要问题,我知道如何调用函数:
do_something(2,5)
而且功能是
int do_something (int arg1,int arg2)
{
int res = arg1 + arg2
return res
}
我不知道如何从我的main.cpp中获取来自其他cpp文件中的函数的函数的“状态”结果:
void HotplugWatcher_GUI::slotDeviceAdded(const QString &dev)
{
qDebug("tid=%#x: add %s", (quintptr) QThread::currentThreadId(), qPrintable(dev));
state->setText("<font color=#0000ff>Add: </font>" + dev);
tray->showMessage(tr("New device"), dev);
}
所以,如果有人能帮我理解,我会很感激
谢谢你的帮助
如果我理解正确,您希望访问该类的“州”成员。
你最好的选择是在类中创建另一个函数来检索它。例如:
QLabel* HotplugWatcher_GUI::getState() const
{
return state;
}
但那有点难看。如果您感兴趣的只是文本,那么您可以这样做:
QString HotplugWatcher_GUI::getStateText() const
{
return state->text();
}