我设计了一个应用程序是C ++ / Qt,用于在我的Mac和Android设备之间传输文件。
要做到这一点,我创建了一个类QDialog,如下所示:
dialog.cpp
Dialog::Dialog(QWidget *parent)
: QWidget(parent)
{
}
void Dialog::CreateProgressBar() {
ProgressDialog = new QWidget(this);
ProgressDialog->setWindowTitle("Progress");
ProgressLayout = new QVBoxLayout(this);
ProgressIndicator = new QProgressBar();
ProgressIndicator->resize(200,25);
ProgressIndicator->setValue(0);
ProgressIndicator->setOrientation(Qt::Horizontal);
//connect(this,SIGNAL(ProgressBar(const uint64_t, const uint64_t, void const * const)),ProgressIndicator,SLOT(setValue(int)));
CancelButton = new QPushButton();
CancelButton->setFixedSize(25,25);
CancelButton->setText("Cancel");
CancelButton->setFlat(true);
CancelButton->setAutoFillBackground(true);
CancelButton->setStyleSheet("QPushButton { background-color : white;}");
connect(CancelButton, SIGNAL(clicked()), this, SLOT(onCancelButtonAction()));
ProgressLayout->addWidget(ProgressIndicator);
ProgressLayout->addWidget(CancelButton);
ProgressDialog->setLayout(ProgressLayout);
ProgressDialog->show();
}
void Dialog::setValue(const uint64_t value) {
ProgressIndicator->setValue(value);
}
void Dialog::DestroyProgressBar() {
ProgressDialog->close();
}
dialog.h
class Dialog : public QWidget
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
/* ProgressIndicator */
//int ProgressBar(const uint64_t data_sent, const uint64_t data_total, void const * const data);
void CreateProgressBar();
void DestroyProgressBar();
int createOverwriteDialogBox(char* filename);
void setValue(const uint64_t value);
void WaitBoxDialog();
void DestroyWaitBoxDialog();
private:
QWidget *ProgressDialog;
QProgressBar *ProgressIndicator;
QVBoxLayout *ProgressLayout;
QPushButton *CancelButton;
要显示它,我目前有一个名为wrapper.cpp的源代码,它负责管理副本。
管理复制时,以及在核对API LIBMTP_Send_File_to_File之前,我开始进行方法实例化。
Dialog *MyProgress = new Dialog();
MyProgress->CreateProgressBar();
genfile = LIBMTP_new_file_t();
LIBMTP_Send_File_From_File(PulsDeviceMngr->device, strdup(AbsolutePath), genfile, NULL, NULL);//ProgressBar, MyProgress);
LIBMTP_destroy_file_t(genfile);
MyProgress->DestroyProgressBar();
//delete MyProgress;
}
我仍然不明白为什么我看不到对话框。
答案 0 :(得分:0)
我认为你需要两个线程,一个用于处理,另一个用于保持GUI运行。像这样:
#include <QFutureWatcher>
#include <QMetaObject>
#include <QtConcurrent/QtConcurrentRun>
void sendFile()
{
LIBMTP_Send_File_From_File(PulsDeviceMngr->device, strdup(AbsolutePath), genfile, NULL, NULL);
QMetaObject::invokeMethod(MyProgress, "setValue", Q_ARG(int, 50));
LIBMTP_destroy_file_t(genfile);
}
MyProgress->CreateProgressBar();
QFutureWatcher<void> futureWatcher;
connect(futureWatcher, SIGNAL(finished()), MyProgress, SLOT(DestroyProgressBar()));
futureWatcher.setFuture(QtConcurrent::run(sendFile));
当然,您需要制作setValue和DestroyProgressBar公共广告位。