我想从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
}
我不知道如何从另一个cpp文件中的函数获取main.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);
}
所以如果有人可以帮助我理解我会很感激
谢谢您的帮助
答案 0 :(得分:1)
如果我理解正确,则希望访问该类的“州”成员。
最好的选择是在要检索它的类中创建另一个函数。例如:
QLabel* HotplugWatcher_GUI::getState() const
{
return state;
}
但这有点丑陋。如果您只对文本感兴趣,则可以执行以下操作:
QString HotplugWatcher_GUI::getStateText() const
{
return state->text();
}