2015-01-17 9 views
0

У меня есть приложение для работы с C++/Qt для передачи файла между моим Mac на Android-устройство.Невозможно отобразить QDialog/QProgressbar

, чтобы сделать это, я создал класс 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; 
} 

Я все еще не понимаю, почему я не мог видеть диалог.

+1

По какой-либо причине вы не можете использовать встроенную функцию [QProgressDialog] (http://doc.qt.io/qt-5/qprogressdialog.html)? – Nazar554

+0

нет настоящая причина. Я попробую. Это мое первое приложение в Qt, и я использовал разные примеры из Интернета, чтобы помочь. благодаря – Seb

ответ

0

Думаю, вам нужны два потока, один для обработки, а другой - для работы графического интерфейса. Что-то вроде этого:

#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 открытые слоты, конечно.