0

I have design an app is C++/Qt to transfer file between my Mac to Android Device.

to do this, I have created a class QDialog as shown below:

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;

To display it, I'm currently having a source code called wrapper.cpp which is in charge on managing the copy.

When managing the Copy, and before colling the API LIBMTP_Send_File_to_File, I start the method instantiation.

   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;
}

I'm still don't understand why I couldn't see the dialog.

Seb
  • 2,929
  • 4
  • 30
  • 73
  • 1
    any reason you can't use built in [QProgressDialog](http://doc.qt.io/qt-5/qprogressdialog.html)? – Nazar554 Jan 17 '15 at 17:02
  • no real reason. I will try. It's my first app in Qt and I took different examples coming from Internet to help. Thanks – Seb Jan 17 '15 at 17:24

1 Answers1

0

I think you need two threads, one to do your processing and the other one to keep the GUI running. Something like this:

#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));

You'll need to make setValue and DestroyProgressBar public slots, of course.

Alex Henrie
  • 744
  • 1
  • 6
  • 17