I was wondering if anybody could tell me whats actually going on here. I have included the header and implementation file for a simple config dialog. The problem is in the updateAutoSaveGroupBox slot I cannot access or change any properties of my widgets on the page. I.E. I want to make some widgets disabled if a check box has not been checked but when I try to set them I get a read access violation. Any help with this matter would be greatly appreciated. The problems exsist in these two lines(commented out so it will run for now without throwing an exception).
//autoSaveLabel->setDisabled(autoSaveIsEnabled);
//autoSaveSpinBox->setDisabled(getAutoSaveIsEnabled());
configWidget.h
class EnigmaConfigGeneralEnvironmentWidget : public QWidget
{
Q_OBJECT
public:
explicit EnigmaConfigGeneralEnvironmentWidget(QWidget *parent = 0);
~EnigmaConfigGeneralEnvironmentWidget();
signals:
void setAutoSaveIsEnabledSignal(bool autoSaveIsEnabled);
public slots:
void setAutoSaveIsEnabled(bool autoSaveIsEnabled){_AutoSaveIsenabled = autoSaveIsEnabled;}
bool getAutoSaveIsEnabled(){return _AutoSaveIsenabled;}
void updateAutoSaveGroupBox(bool autoSaveIsEnabled);
private:
void makeConnections();
void readSettings();
void writeSettings();
void createMainWidget();
QGroupBox *uiGroupBox;
QStringList localList;
QLabel *localLabel;
QComboBox *localeComboBox;
QHBoxLayout *localSelectionHLayout;
QGroupBox *systemGroupBox;
QCheckBox *autoSaveCheckBox;
QLabel *autoSaveLabel;
QSpinBox *autoSaveSpinBox;
QHBoxLayout *autoSaveHLayout;
bool _AutoSaveIsenabled;
};
ConfigWidget.cpp
#include "enigmaconfiggeneralenvironmentwidget.h"
#include <QtWidgets>
EnigmaConfigGeneralEnvironmentWidget::EnigmaConfigGeneralEnvironmentWidget(QWidget *parent) :
QWidget(parent)
{
makeConnections();
readSettings();
createMainWidget();
}
EnigmaConfigGeneralEnvironmentWidget::~EnigmaConfigGeneralEnvironmentWidget()
{
writeSettings();
}
void EnigmaConfigGeneralEnvironmentWidget::makeConnections()
{connect(this,SIGNAL(setAutoSaveIsEnabledSignal(bool)),this,SLOT(setAutoSaveIsEnabled(bool)) );
connect(this,SIGNAL(setAutoSaveIsEnabledSignal(bool)),this,SLOT(updateAutoSaveGroupBox(bool)) );
}
void EnigmaConfigGeneralEnvironmentWidget::readSettings()
{
QSettings settings;
settings.beginGroup(tr("UI.Config.Environment.General"));
bool autoSaveIsEnabled = settings.value("autoSaveIsEnabled",bool(true)).toBool();
setAutoSaveIsEnabledSignal(autoSaveIsEnabled);
settings.endGroup();
}
void EnigmaConfigGeneralEnvironmentWidget::writeSettings()
{
QSettings settings;
settings.beginGroup(tr("UI.Config.Environment.General"));
settings.setValue("autoSaveIsEnabled",getAutoSaveIsEnabled());
settings.endGroup();
}
void EnigmaConfigGeneralEnvironmentWidget::createMainWidget()
{
localList.append(tr("Danish - ???"));
localList.append(tr("English - Australia"));
localList.append(tr("English - Canada"));
localList.append(tr("English - USA"));
localList.append(tr("English - UK"));
localList.append(tr("Finnish - Finland"));
localList.append(tr("French - Canada"));
localList.append(tr("French - France"));
localList.append(tr("Norwegian - ???"));
localList.append(tr("Swedish - ???"));
uiGroupBox = new QGroupBox();
uiGroupBox->setTitle(tr("UI Settings"));
localLabel= new QLabel();
localLabel->setText(tr("Select a language: "));
localeComboBox = new QComboBox();
localeComboBox->addItems(localList);
localSelectionHLayout = new QHBoxLayout(uiGroupBox);
localSelectionHLayout->addWidget(localLabel);
localSelectionHLayout->addWidget(localeComboBox);
systemGroupBox = new QGroupBox();
systemGroupBox->setTitle(tr("System Settigns"));
autoSaveCheckBox = new QCheckBox();
autoSaveCheckBox->setText(tr("Auto-Save Enabled: "));
autoSaveCheckBox->setChecked(getAutoSaveIsEnabled());
connect(autoSaveCheckBox,SIGNAL(clicked(bool)),this,SIGNAL(setAutoSaveIsEnabledSignal(bool)));
autoSaveLabel = new QLabel(this);
autoSaveLabel->setText(tr("Auto-Save Interval is Every: "));
autoSaveSpinBox = new QSpinBox();
autoSaveSpinBox->setSuffix(tr("Mins."));
autoSaveSpinBox->setAccelerated(true);
autoSaveHLayout = new QHBoxLayout(systemGroupBox);
autoSaveHLayout->addWidget(autoSaveCheckBox);
autoSaveHLayout->addWidget(autoSaveLabel);
autoSaveHLayout->addWidget(autoSaveSpinBox);
QVBoxLayout *vLayout = new QVBoxLayout(this);
vLayout->addWidget(uiGroupBox);
vLayout->addWidget(systemGroupBox);
}
void EnigmaConfigGeneralEnvironmentWidget::updateAutoSaveGroupBox(bool autoSaveIsEnabled)
{
qDebug() << "debug " << autoSaveIsEnabled;
//autoSaveLabel->setDisabled(autoSaveIsEnabled);
//autoSaveSpinBox->setDisabled(getAutoSaveIsEnabled());
}
Spotted it:
void EnigmaConfigGeneralEnvironmentWidget::readSettings()
{
QSettings settings;
settings.beginGroup(tr("UI.Config.Environment.General"));
bool autoSaveIsEnabled = settings.value("autoSaveIsEnabled",bool(true)).toBool();
setAutoSaveIsEnabledSignal(autoSaveIsEnabled);
settings.endGroup();
}
You are calling void setAutoSaveIsEnabledSignal(bool) here (the signal), not void setAutoSaveIsEnabled(bool) (the actual setter). Hence the member variable is still uninitialized.
Reminder for yourself: Don't name signals as if they were setters, use e.g. void autoSaveIsEnabledChanged(bool)
Related
So lets say I a QDialog class like this
class DiagExample : public QDialog
{
Q_OBJECT
public:
DiagExample(QWidget *parent);
private:
int myIntValue = 0;
QPushButton *AddToValue;
QPushButton *MinusToValue;
QLabel *counter;
};
And the implementation looks like this
DiagExample::DiagExample(QWidget *parent) : QDialog(parent)
{
setWindowTitle("Example Diag");
QVBoxLayout *layout = new QVBoxLayout(this);
AddToValue = new QPushButton(tr("Add"));
MinusToValue = new QPushButton(tr("Minus"));
counter = new QLabel;
connect(AddToValue, &QPushButton::released, [=](){myIntValue++;});
connect(MinusToValue, &QPushButton::released, [=](){myIntValue--;});
layout->addWidget(AddToValue);
layout->addWidget(MinusToValue);
layout->addWidget(counter);
// below is what I want to achieve but have no idea how
// essentially when value of the int is changed, text of
// counter (QLabel) will be set to the new value
connect(myIntValue, ???, [=](int newValue){
counter->setText(QString::number(newValue);});
}
I know that I could go straight from QPushButton::released -> setText on the QLabel, but my code will eventually have many inputs feeding into the counter, so from a point of readability and simplicity, I would rather have this sort of paradigm --- INPUT_WIDGET -> myIntValue -> setText(NEW VALUE OF myIntValue).
Standard way of doing such things in Qt:
class DiagExample : public QDialog
{
Q_OBJECT
Q_PROPERTY(intValue READ intValue NOTIFY onIntValueChange)
public:
DiagExample(QWidget *parent);
int intValue() const;
signals:
void onIntValueChange(int);
private:
int myIntValue = 0;
QPushButton *AddToValue;
QPushButton *MinusToValue;
QLabel *counter;
};
Now in cpp:
DiagExample::DiagExample(QWidget *parent) : QDialog(parent)
{
setWindowTitle("Example Diag");
QVBoxLayout *layout = new QVBoxLayout(this);
AddToValue = new QPushButton(tr("Add"));
MinusToValue = new QPushButton(tr("Minus"));
counter = new QLabel;
connect(AddToValue, &QPushButton::released, [=](){
myIntValue++;
emit onIntValueChange(myIntValue);
});
connect(MinusToValue, &QPushButton::released, [=](){
myIntValue--;
emit onIntValueChange(myIntValue);
});
layout->addWidget(AddToValue);
layout->addWidget(MinusToValue);
layout->addWidget(counter);
// below is what I want to achieve but have no idea how
// essentially when value of the int is changed, text of
// counter (QLabel) will be set to the new value
connect(this, &DiagExample::onIntValueChange, [=](int newValue){
counter->setText(QString::number(newValue);});
}
int DiagExample::intValue() const {
return myIntValue;
}
Note only QObject can emit signals!
You will have to define it yourself, something like that could work:
class DiagExample : public QDialog
{
Q_OBJECT
public:
DiagExample(QWidget *parent);
private:
int myIntValue = 0;
QPushButton *AddToValue;
QPushButton *MinusToValue;
QLabel *counter;
private slots:
void onPlusOne();
void onMinusOne();
signals:
void intValueChanged(const QString& newVal);
};
and then in you cpp file:
DiagExample::DiagExample(QWidget *parent) : QDialog(parent)
{
setWindowTitle("Example Diag");
QVBoxLayout *layout = new QVBoxLayout(this);
AddToValue = new QPushButton(tr("Add"));
MinusToValue = new QPushButton(tr("Minus"));
counter = new QLabel;
connect(AddToValue, &QPushButton::released, &DiagExample::onPlusOne);
connect(MinusToValue, &QPushButton::released, &DiagExample::onMinusOne);
connect(this, &DiagExample::intValueChanged, counter, &QLabel::setText);
layout->addWidget(AddToValue);
layout->addWidget(MinusToValue);
layout->addWidget(counter);
// below is what I want to achieve but have no idea how
// essentially when value of the int is changed, text of
// counter (QLabel) will be set to the new value
connect(myIntValue, ???, [=](int newValue){
counter->setText(QString::number(newValue);});
}
void DiagExample::onPlusOne()
{
myIntValue++;
emit intValueChanged(QString::number(myIntValue));
}
void DiagExample::onMinusOne()
{
myIntValue--;
emit intValueChanged(QString::number(myIntValue));
}
I am trying to run some code when I select a new index in a QTreeView
In RoverPlanner.h
namespace Ui {
class RoverPlanner;
}
class RoverPlanner : public QWidget
{
Q_OBJECT
public:
explicit RoverPlanner(QWidget *parent = nullptr);
void save_paths_from_tree(QTreeView* treeView);
void load_paths_into_tree(QTreeView* treeView);
std::vector<cuarl_path::Path> get_paths(const char* filename) const;
void update_segment_editor();
cuarl_path::Segment* addSegment();
~RoverPlanner();
private Q_SLOTS:
void treeSelectionChanged(const QModelIndex& prevIndex, const QModelIndex& nextIndex);
private:
Ui::RoverPlanner *ui;
};
In RoverPlanner.cpp
RoverPlanner::RoverPlanner(QWidget *parent) :
QWidget(parent),
ui(new Ui::RoverPlanner)
{
ui->setupUi(this);
QPushButton* btnLoadPaths = this->findChild<QPushButton*>("btn_load_paths");
QPushButton* btnSavePaths = this->findChild<QPushButton*>("btn_save_paths");
QPushButton* btnExecutePath = this->findChild<QPushButton*>("btn_execute_path" );
QPushButton* btnAddSegment = this->findChild<QPushButton*>("btn_add_to_path");
QTreeView* treeView = this->findChild<QTreeView*>("tree_paths");
connect(btnLoadPaths, &QPushButton::clicked, this, [=]() { load_paths_into_tree(treeView); });
connect(treeView->selectionModel(), &QItemSelectionModel::currentChanged, this, &RoverPlanner::treeSelectionChanged); // This does not seem to properly bind the index chan
}
void RoverPlanner::treeSelectionChanged(const QModelIndex &prevIndex, const QModelIndex &nextIndex) {
std::cout << "Test" << std::endl;
}
//other functions
When I click on the items, it does not output anything in the console
I'm confused because this seems to be the way to correctly connect the treeview selected index changed. What did I do wrong?
selectionModel gets replaced each time a new model is set for QTreeView.
void QAbstractItemView::setModel(QAbstractItemModel *model):
This function will create and set a new selection model, replacing any model that was previously set with setSelectionModel().
That means you need to reconnect the &QItemSelectionModel::currentChanged signal each time you set a new model.
I need to create a login dialog like this
image files: eyeOn.png, eyeOff.png
Requirements:
password is shown only when we CLICK AND HOLD the eyeOn icon (even when we click and hold and drag the mouse to area outside of the dialog, the password is still shown), when we release the mouse, password is covered again.
while password is shown, the eye icon is eyeOn. While password is covered, the eye icon is eyeOff.
I have just built the layout.
QGridLayout *mainlogin = new QGridLayout();
QLabel *usernameLabel = new QLabel;
usernameLabel->setWordWrap(true);
usernameLabel->setText("Username");
mainlogin->addWidget(usernameLabel, 0, 0);
QComboBox *usernameLineEdit = new QComboBox;
usernameLineEdit->setEditable(true);
usernameLabel->setBuddy(usernameLineEdit);
mainlogin->addWidget(usernameLineEdit, 0, 1);
QLabel *capslockShow = new QLabel;
capslockShow->setWordWrap(true);
capslockShow->setText(" ");
mainlogin->addWidget(capslockShow, 1, 1);
QLabel *passwordLabel = new QLabel;
passwordLabel->setWordWrap(true);
passwordLabel->setText("Password");
mainlogin->addWidget(passwordLabel, 2, 0);
QLineEdit *passwordLineEdit = new QLineEdit;
passwordLineEdit->setEchoMode(QLineEdit::Password);
QAction *myAction = passwordLineEdit->addAction(QIcon(":/eyeOff.png"), QLineEdit::TrailingPosition);
passwordLabel->setBuddy(passwordLineEdit);
mainlogin->addWidget(passwordLineEdit, 2, 1);
What should I do next? Pls help me with code snippet.
The solution is to add a QAction to the QLineEdit, this will create a QToolButton that we can obtain from the associatedWidgets() (it will be the second widget since the first one is the one associated with clearButton). Already having the QToolButton you must use the pressed and released signal.
passwordlineedit.h
#ifndef PASSWORDLINEEDIT_H
#define PASSWORDLINEEDIT_H
#include <QAction>
#include <QLineEdit>
#include <QToolButton>
class PasswordLineEdit: public QLineEdit
{
public:
PasswordLineEdit(QWidget *parent=nullptr);
private slots:
void onPressed();
void onReleased();
protected:
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);
void focusInEvent(QFocusEvent *event);
void focusOutEvent(QFocusEvent *event);
private:
QToolButton *button;
};
#endif // PASSWORDLINEEDIT_H
passwordlineedit.cpp
#include "passwordlineedit.h"
PasswordLineEdit::PasswordLineEdit(QWidget *parent):
QLineEdit(parent)
{
setEchoMode(QLineEdit::Password);
QAction *action = addAction(QIcon(":/eyeOff"), QLineEdit::TrailingPosition);
button = qobject_cast<QToolButton *>(action->associatedWidgets().last());
button->hide();
button->setCursor(QCursor(Qt::PointingHandCursor));
connect(button, &QToolButton::pressed, this, &PasswordLineEdit::onPressed);
connect(button, &QToolButton::released, this, &PasswordLineEdit::onReleased);
}
void PasswordLineEdit::onPressed(){
QToolButton *button = qobject_cast<QToolButton *>(sender());
button->setIcon(QIcon(":/eyeOn"));
setEchoMode(QLineEdit::Normal);
}
void PasswordLineEdit::onReleased(){
QToolButton *button = qobject_cast<QToolButton *>(sender());
button->setIcon(QIcon(":/eyeOff"));
setEchoMode(QLineEdit::Password);
}
void PasswordLineEdit::enterEvent(QEvent *event){
button->show();
QLineEdit::enterEvent(event);
}
void PasswordLineEdit::leaveEvent(QEvent *event){
button->hide();
QLineEdit::leaveEvent(event);
}
void PasswordLineEdit::focusInEvent(QFocusEvent *event){
button->show();
QLineEdit::focusInEvent(event);
}
void PasswordLineEdit::focusOutEvent(QFocusEvent *event){
button->hide();
QLineEdit::focusOutEvent(event);
}
The complete example can be downloaded from the following link.
Rather than trying to make use of the QAction returned by QLineEdit::addAction you could probably use a QWidgetAction for this when combined with a suitable event filter...
class eye_spy: public QWidgetAction {
using super = QWidgetAction;
public:
explicit eye_spy (QLineEdit *control, QWidget *parent = nullptr)
: super(parent)
, m_control(control)
, m_on(":/eyeOn")
, m_off(":/eyeOff")
, m_pixmap_size(50, 50)
{
m_label.setScaledContents(true);
m_control->setEchoMode(QLineEdit::Password);
m_label.setPixmap(m_off.pixmap(m_pixmap_size));
m_label.installEventFilter(this);
setDefaultWidget(&m_label);
}
protected:
virtual bool eventFilter (QObject *obj, QEvent *event) override
{
if (event->type() == QEvent::MouseButtonPress) {
m_control->setEchoMode(QLineEdit::Normal);
m_label.setPixmap(m_on.pixmap(m_pixmap_size));
} else if (event->type() == QEvent::MouseButtonRelease) {
m_control->setEchoMode(QLineEdit::Password);
m_label.setPixmap(m_off.pixmap(m_pixmap_size));
}
return(super::eventFilter(obj, event));
}
private:
QLineEdit *m_control;
QLabel m_label;
QIcon m_on;
QIcon m_off;
QSize m_pixmap_size;
};
Now, rather than...
QAction *myAction = passwordLineEdit->addAction(QIcon(":/eyeOff.png"), QLineEdit::TrailingPosition);
Use...
eye_spy eye_spy(passwordLineEdit);
passwordLineEdit->addAction(&eye_spy, QLineEdit::TrailingPosition);
Seems to provide the desired behaviour.
I have a problem connecting context menu actions in Qt. I know there are plenty of similar questions around, nevertheless I couldn't find a solution yet.
I have a series of plots built using QCustomplot.
What I want to do is to create a context menu when right-clicking on the background of each plot listing all the signal present in the graph. By clicking an entry of this menu, the corresponding signal should be hidden (if currently visible) or made visible (if hidden).
Now, I've defined a class called PlotHandler of which I paste the relevant parts here below:
plotHandler.cpp
#include "plothandler.h"
PlotHandler::PlotHandler(QStringList groupNames, int startIdx, QWidget *parent) :
QWidget(parent), scrolling(false), refreshing(true)
{
pPlot = new QCustomPlot(this);
pPlot->setContextMenuPolicy(Qt::CustomContextMenu);
connect(pPlot, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint)));
}
void PlotHandler::contextMenuRequest(QPoint pos)
{
int i;
QMenu *menu = new QMenu(this);
for(i=0; i<pGroup->getDataLength(); i++)
{
QAction *menuEntry;
menuEntry = new QAction(pPlot->graph(i)->name(), this);
menuEntry->setProperty("graphIdx", i);
menu->addAction(menuEntry);
connect(menuEntry, SIGNAL(triggered()), this, SLOT(addRemoveGraph()));
}
menu->popup(pPlot->mapToGlobal(pos));
}
void PlotHandler::addRemoveGraph()
{
QAction *selectedSignal = qobject_cast<QAction *>(sender());
int tmp = selectedSignal->property("graphIdx").toInt();
if (pPlot->graph(tmp)->visible())
{
pPlot->graph(tmp)->setVisible(false);
}
else
{
pPlot->graph(tmp)->setVisible(true);
}
}
plotHandler.h
class PlotHandler : public QWidget
{
Q_OBJECT
public:
explicit PlotHandler(QStringList groupNames, int startIdx, QWidget *parent = 0);
QString groupRequested();
private:
QCustomPlot *pPlot;
public slots:
void contextMenuRequest(QPoint pos);
void addRemoveGraph();
}
The menu is correctly showed with the right entries and when I click on an action addRemoveGraph is called. In debug it gives back the following message:
The inferior stopped because it triggered an exception. Stopped in
thread 0 by: Exception at 0x5d6c2f9a, code: 0xc0000005: read access
violation at: 0x0, flags=0x0.
trying to execute
int tmp = selectedSignal->property("graphIdx").toInt();
Could anyone point me towards the right direction?
Thanks in advance
You use QObject::setProperty but QAction doesn't have a property named "graphIdx". When you attempt to read the "graphIdx" property from QAction you will always get an invalid QVariant.
int tmp = selectedSignal->property("graphIdx").toInt();
// tmp always is 0;
You can use QAction::setData if you need to store only one property. Otherwise, use QObject::setProperty to set a custom property on any QObject. QAction is a QObject.
Here below the solution that solved the issue.
plotHandler.cpp
#include "plothandler.h"
PlotHandler::PlotHandler(QStringList groupNames, int startIdx, QWidget *parent) :
QWidget(parent), scrolling(false), refreshing(true)
{
pPlot = new QCustomPlot(this);
pPlot->setContextMenuPolicy(Qt::CustomContextMenu);
connect(pPlot, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint)));
}
void PlotHandler::contextMenuRequest(QPoint pos)
{
int i;
QMenu *menu = new QMenu(this);
for(i=0; i<pGroup->getDataLength(); i++)
{
QAction *menuEntry;
menuEntry = new QAction(pPlot->graph(i)->name(), this);
menuEntry->setData(i);
menu->addAction(menuEntry);
}
connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(addRemoveGraph(QAction*)));
menu->popup(pPlot->mapToGlobal(pos));
}
void PlotHandler::addRemoveGraph(QAction *selectedSignal)
{
int tmp = selectedSignal->property("graphIdx").toInt();
if (pPlot->graph(tmp)->visible())
{
pPlot->graph(tmp)->setVisible(false);
}
else
{
pPlot->graph(tmp)->setVisible(true);
}
pPlot->replot();
}
plotHandler.h
class PlotHandler : public QWidget
{
Q_OBJECT
public:
explicit PlotHandler(QStringList groupNames, int startIdx, QWidget *parent = 0);
QString groupRequested();
private:
QCustomPlot *pPlot;
public slots:
void contextMenuRequest(QPoint pos);
void addRemoveGraph();
}
Thanks everyone for your help.
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.
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.