Consider this SLOT, in my main thread, trigged by a button, which takes a list of QTreeWidgetItem from a QTreeWidget. It uses a QtConcurrent::map() call to execute a long task. The watcher connects to a QProgressDialog to show progress.
void Main::on_actionButton_triggered() {
qRegisterMetaType<QVector<int> >("QVector<int>");
//Setting up a progress dialog
QProgressDialog progressDialog;
//Holds the list
QList<QTreeWidgetItem*> list;
//Setup watcher
QFutureWatcher<void> watcher;
//Setting up connections
//Progress dialog
connect(&watcher, SIGNAL(progressValueChanged(int)), &progressDialog, SLOT(setValue(int)));
connect(&watcher, SIGNAL(progressRangeChanged(int, int)), &progressDialog, SLOT(setRange(int,int)));
connect(&watcher, SIGNAL(progressValueChanged(int)), ui->progressBar, SLOT(setValue(int)));
connect(&watcher, SIGNAL(progressRangeChanged(int, int)), ui->progressBar, SLOT(setRange(int,int)));
connect(&watcher, SIGNAL(finished()), &progressDialog, SLOT(reset()));
connect(&progressDialog, SIGNAL(canceled()), &watcher, SLOT(cancel()));
connect(&watcher, SIGNAL(started()), this, SLOT(processStarted()));
connect(&watcher, SIGNAL(finished()), this, SLOT(processFinished()));
//Gets the list filled
for (int i = 0; i < ui->listTreeWidget->topLevelItemCount(); i++) {
list.append(ui->listTreeWidget->topLevelItem(i));
}
//And start
watcher.setFuture(QtConcurrent::map(list, processRoutine));
//Show the dialog
progressDialog.exec();
}
extern void processRoutine(QTreeWidgetItem* item) {
qDebug() << item->text(4);
}
I also added, in the UI (which holds all the previous widgets), a QProgressBar with the same SIGNALS/SLOTS. Keeping the code like the previous works as expected: the progress dialog shows up, and the progress bar updates exactly as the dialog.
Instead, if I comment the
//progressDialog.exec();
or I hide the dialog in some way, the process crashes (not always, sometimes it goes well). Looking at qDebug() << item->text(4); it crashes and the output shows randomly mixed text (they are supposed to be filenames). Also, the progressbar does not update itself when the QProgressDialog is not shown, even if the computation does not crash.
Note: I experienced a similar problem before in another function, and I solved it by setting
QThreadPool::globalInstance()->setMaxThreadCount(1);
on Windows only, OSX was ok.
So, what's the trick behind the QProgressDialog that makes all the things right? Is there a way I can use the QProgressBar instead of the QProgressDialog?
NOTE
This is the output when the process completes without troubles:
"C:/Users/Utente/Pictures/Originals/unsplash_52cee67a5c618_1.jpg"
"C:/Users/Utente/Pictures/Originals/photo-1428278953961-a8bc45e05f72.jpg"
"C:/Users/Utente/Pictures/Originals/photo-1429152937938-07b5f2828cdd.jpg"
"C:/Users/Utente/Pictures/Originals/photo-1429277158984-614d155e0017.jpg"
"C:/Users/Utente/Pictures/Originals/photo-1430598825529-927d770c194f.jpg"
"C:/Users/Utente/Pictures/Originals/photo-1433838552652-f9a46b332c40.jpg"
When you comment progressDialog.exec();
your on_actionButton_triggered() function finishes with destroying progressDialog so your signals are using pointers to nowhere.
Also watcher is being destroyed too before or after all mapping is performed, and afair it does not stop threads so they also working with nowhere.
Related
I have a QFutureWatcher connected to a QProgressBar, the code is running but never show me the progress of the progressBar
QProgressDialog progress;
QFutureWatcher<void> watcher;
connect(&watcher, SIGNAL(finished()),&progress, SLOT(reset()));
connect(&watcher, SIGNAL(progressRangeChanged(int, int)),&progress, SLOT(setRange(int,int)));
connect(&watcher, SIGNAL(progressValueChanged()),&progress, SLOT(setValue(int)));
QFuture<int> file = QtConcurrent::run(aFunction, cmd);
watcher.setFuture(file);
progress.exec();
watcher.waitForFinished();
int aFunction(string cmd){
int status;
status= system(cmd.c_str());
return status;
}
this is the code I have, the result just show me a full progress bar, I want to see from 0 to 100
I have listwidgetitem's that have a custom widget (step_widget) which contains 3 QPushButtons and a QLabel.
When I press one of the buttons in the widget, it is not triggering itemClicked which i need to see the index of the listwidget that was clicked. If i click the QLabel, the signal is triggered and i am able to obtain the index. How can trigger the signal when one of the buttons is pressed? Why does it not trigger?
QListWidgetItem *item = new QListWidgetItem();
stepWidget *step_widget = new stepWidget();
ui->listWidget->addItem(item);
ui->listWidget->setItemWidget(item,step_widget);
The itemClicked() signal is not emmited because the QPushButton consumes the click event.
There is the simplest solution I've found.
First, in your class stepWidget add a signal that will be emitted when any QPushButton is clicked, for example:
signals:
void widgetClicked();
Then connect the clicked() signal of every QPushButton with that signal. This code is in the constructor of the widget stepWidget:
connect(ui->pushButton1, &QPushButton::clicked, this, &stepWidget::widgetClicked);
connect(ui->pushButton2, &QPushButton::clicked, this, &stepWidget::widgetClicked);
connect(ui->pushButton3, &QPushButton::clicked, this, &stepWidget::widgetClicked);
To test it I added 10 widgets to a QListWidget that reside in the MainWindow class. You must connect that signal to a slot (in this case I use a C++11 Lambda Function, but you can create a slot instead, it's fine) where you establish the current item as the item under the widget. This code is located in the constructor of MainWindow:
for (int i = 0; i < 10; ++i) {
QListWidgetItem *item = new QListWidgetItem;
stepWidget *step_Widget = new stepWidget;
ui->listWidget->addItem(item);
ui->listWidget->setItemWidget(item, step_Widget);
connect(step_Widget, &stepWidget::widgetClicked, [=]() {
ui->listWidget->setCurrentItem(item);
});
}
Now, this will not make the itemClicked() signal to be emitted because the user not clicked the item. But I advice you to connect instead the signal currentRowChanged() as it will be emitted with this code and also it has the advantage that it allows you to directly obtain the row. Once you have the row you can obtain the item and the widget, evidently.
However, this procedure has the inconvenient that the currentRowChanged() signal will be emitted even when the user selects a row with the keyboard or when the user press and slide the mouse over the QListWidget without releasing it.
On workaround could be, for example, using a flag as an attribute for your MainWindow class that is always false except during this connection:
connect(ste_pWidget, &stepWidget ::widgetClicked, [=]() {
buttonPressed = true;
ui->listWidget->setCurrentItem(item);
buttonPressed = false;
});
Then you must manipulate two signals from the QListWidget: clicked() and currentRowChanged(). I choose clicked() instead of itemClicked() because it gives almost directly access to the row:
void MainWindow::on_listWidget_clicked(const QModelIndex &index)
{
ui->statusBar->showMessage(tr("Row clicked: %1").arg(index.row()), 1000);
}
void MainWindow::on_listWidget_currentRowChanged(int currentRow)
{
if (buttonPressed) {
ui->statusBar->showMessage(tr("Row clicked: %1").arg(currentRow), 1000);
}
}
what you can simply do is, make your qpushbutton to qlabel and then qpushbutton will become clickable.
You can then override this method :- on_listWidget_itemClicked(QListWidgetItem *item) for double click on qpushbutton.
I tried it & it worked perfectly, its quite simple workaround.
I am trying to close a QDialog using a timeout from a QTimer.
So far, i have tried to do this :
QDialog dlg;
..
..
myTimer.start(60000); // 60 s
connect(&myTimer, SIGNAL(timeout()),
&dlg, SLOT(close())));
dlg.exec();
qWarning() << "---timer expired or key pressed--";
But when timeout is triggered and the close slot executed the eventloop is not exited. Same behavior with reject slot. I know the done slot should have the expected behavior but as it needs an extra argument (int r), it cannot be directly connected to the timeout() signal.
Of course, i can "relay" the timeout signal to provide the missing argument but is there another more straightforward way to do it ?
Thank you.
dlg.exec(); Is a synchronic, He returns the answer accepted or rejected.
void MainWindow::btnClicked() {
Dialog *dialog = new Dialog();
dialog.exec();
qDebug() << "test";
// while dialog not destroyed (rejected, accepted) Print will not happen never.
}
One way you can use QTimer in your Dialog class:
Dialog::dialog(...) {
//constructor
QTimer::singleShot(60000, this, SLOT(close()));
}
or do not use dialog.exec(); use dialog.show(); if you want dialog let it be modality you can use:
void MainWindow::btnClicked() {
Dialog *dialog = new Dialog();
dialog->setModal(true);
dialog->show();
qDebug() << "test"; //The "test" will be printed, and you can use QTimer :))
}
I suggest to give the dialog its own timer (i.e. instantiate a QTimer locally, before excuting the dialog):
QTimer dlg_timer;
dlg_timer.start(60000); // 60 s
connect(&dlg_timer, SIGNAL(timeout()), &dlg, SLOT(close()));
dlg.exec();
dlg_timer.stop();
As the OP fears in their comment, if the timer timeout signal has been connected to some other slot, before connection with dialog close, and in that slot QTimer::disconnect() is called, the dialog close slot will never be called.
The dialog opens with two buttons, OK and Cancel. None of the buttons responds to user click. I have to press the X on the top right to cancel the dialog.
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == mTabWidget && event->type() == QEvent::MouseButtonDblClick)
{
// query and set tab(s) names
QTabWidget *tab = qobject_cast<QTabWidget *>(obj);
if(tab)
{
QDialog dlg;
QVBoxLayout la(&dlg);
QLineEdit ed;
la.addWidget(&ed);
QDialogButtonBox bb(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
la.addWidget(&bb);
dlg.setLayout(&la);
if(dlg.exec() == QDialog::Accepted)
{
tab->setTabText(0, ed.text());
return true;
}
}
}
// Standard event processing
return QObject::eventFilter(obj, event);
}
Am I missing any connect() line or signals? I tried to read the Qt documentation, but from what I understood, calling QDialogButtonBox::OK gets processed as Accepted.
UPDATE :
New Dialog Function
OK, i have created a new function that takes care of the Dialog box, i am calling it from the event function. it is still not responding, now on the terminal, i see an error that says, : no such slot MainWindow::accept() and another for reject. I know that i have no slots for these two in the .h file. i tried to find how to build the slots but i couldnt, any help would be great. thank you
void MainWindow::initializeBOX()
{
QDialog dlg;
QVBoxLayout la(&dlg);
QLineEdit ed;
la.addWidget(&ed);
//QDialogButtonBox bb(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
//btnbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
la.addWidget(buttonBox);
dlg.setLayout(&la);
if(dlg.exec() == QDialog::Accepted)
{
mTabWidget->setTabText(0, ed.text());
}
}
Rather than launching the dialog from event filer you should trap QWidget::mouseDoubleClickEvent by overloading that virtual function in your code. And as long as it is a callback already you can do more stuff immediately from there including the dialog. Or maybe send the signal to slot that does the dialog (a bit cleaner). I would do the signal from mouseDoubleClickEvent event handler and make the parent QWidget::mouseDoubleClickEvent to consume the event to avoid possible complications especially when porting the code to other platform.
Event filters are for non-standard event processing. There is nothing non-standard in your case.
I have a Qt Application which i want to show in the System Tray.
My desired behavior is that if the user clicks the close button of the Application than that application hides in the system tray but does not exit.
My code in main.cpp is :
if (QSystemTrayIcon::isSystemTrayAvailable())
{
QObject *root = engine.rootObjects().at(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
QAction *showAction = new QAction(QObject::tr("Show"), window);
window->connect(showAction, SIGNAL(triggered()), window, SLOT(show()));
QAction *hideAction = new QAction(QObject::tr("Hide"), window);
window->connect(hideAction, SIGNAL(triggered()), window, SLOT(hide()));
QAction *quitAction = new QAction(QObject::tr("&Quit"), window);
window->connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
QObject::connect(qApp,SIGNAL(aboutToQuit()),window,SLOT(hide()));
QMenu *trayIconMenu = new QMenu();
trayIconMenu->addAction(showAction);
trayIconMenu->addAction(hideAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
QSystemTrayIcon *trayIcon = new QSystemTrayIcon(window);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setToolTip("xxx");
trayIcon->setIcon(QIcon("xxx.png"));
trayIcon->show();
}
Now i am not able to connect the aboutToQuit signal and hide the application in the tray i.e
QObject::connect(qApp,SIGNAL(aboutToQuit()),window,SLOT(hide())); line is not correct but i am not getting any errors etc.
Apart from this everything is working correctly.Can someone please tell me what i am doing wrong and how can i achieve my desired behavior.
I would also like to know whether i have got the right signal to connect or whether i should try connecting to some other signal.
Thanks in advance.
You can use :
qApp()->setQuitOnLastWindowClosed(false);
quitOnLastWindowClosed property is true by default which causes your application to quit when the last window is closed. By setting it to false, your application does not terminate when you close the main window.
You can also reimplement closeEvent of your main widget, ignore the close event and just hide your window :
void MainWindow::closeEvent(QCloseEvent * e)
{
e->ignore();
this->hide();
}