我设计了一个应用程序是C++/Qt,用于在我的Mac和Android设备之间传输文件。
为此,我创建了一个类QDialog,如下所示:
对话框.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();
}
对话.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;
}
我仍然不明白为什么我看不到对话框。
我认为您需要两个线程,一个用于进行处理,另一个用于保持 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成为公共插槽。