Firstly, for context, I am connecting a signal from triggered from QAction to a slot called fileOpen in this, and other similar connections are done in a method in my main window class like the following:
void MainWindow::createActions()
{
m_fileNew = new QAction("&New", this);
m_fileOpen = new QAction("&Open", this);
m_fileExit = new QAction("E&xit", this);
connect(m_fileNew, SIGNAL(triggered(bool)), this, SLOT(fileNew()));
connect(m_fileOpen, SIGNAL(triggered(bool)), this, SLOT(fileOpen()));
connect(m_fileExit, SIGNAL(triggered(bool)), this, SLOT(fileExit()));
}
To show the file dialog, the static method QFileDialog::getOpenFileName is used in MainWindow::fileOpen:
void MainWindow::fileOpen()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open Audio File"),
"", tr("WAVE Files (*.wav);;All Files (*.*)"));
if (filename != QString::null) {
m_fileName = filename;
}
}
The signal-slot connection between m_fileOpen and fileOpen works and displays the file dialog, but after closing the dialog the window takes longer to redraw while resizing.
Why is that happening, and how can I fix it?
All I had to do was build my Qt application in release mode, which means removing the "CONFIG+=debug" argument from the qmake call.
In release mode the performance degradation is gone, which is great, although I don't understand what differences between the debug and release versions of the Qt libraries allow this to occur.
Related
I am very new to Qt and OpenCV and am creating a project integrating both. The problem I am running into is that I have a button to load a file, which uses QFileDialog. The whole thing runs smoothly and my file gets loaded. However, it crashes if I click the load button a second time. It seems like the problem occurs at the call to QFileDialog::getOpenFileName, but I need an expert opinion.
This is the function for the button click.
void MainWindow::on_pushButton_clicked()
{
QFileDialog dialog(this);
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
dialog.setViewMode(QFileDialog::Detail);
// dialog.setAttribute(Qt::WA_DeleteOnClose);
// dialog.DontUseNativeDialog;
filename = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home",
tr("Images (*.png *.xpm *.jpg)"));
imageObject = new QImage();
imageObject->load(filename);
image = QPixmap::fromImage(*imageObject);
scene = new QGraphicsScene(this);
scene->addPixmap(image);
scene->setSceneRect(image.rect());
ui->graphicsView->setScene(scene);
ui->graphicsView->fitInView(scene->sceneRect(),Qt::KeepAspectRatio);
cvHandler = new OpenCVHandler(filename.toStdString());
}
I have already tried both the lines that are commented out. My search also turned up nothing that I could understand easily:
Crash when calling getOpenFileName from QItemDelegate's custom editor
QFileDialog opens a second (possibly parent) unwanted window
Qt File Dialog Rendered Incorrectly and Crashes
If at all relevant, I am on an Ubuntu 16.04 LTS system.
Thank you
The problem was in the commented lines. I didn't use dialog.DontUseNativeDialog properly. Using it inside the getOpenFileName function did the trick:
filename = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home",
tr("Images (*.png *.xpm *.jpg)"),0,QFileDialog::DontUseNativeDialog);
Thank you all.
When I open an open file dialog the first time everything works as it should, meaning the dialog opens and I can select a certain file. The problem I have is that each time I am trying to open an open file dialog a second time my whole program freezes. I have no idea why this is happening. I have debugged my program and I've seen that each time the program freezes at the dialog.exec() method. This issue started after I have upgraded my qt creator to version 5.8. I have also tried deleting the dialog instance each time I press a button and creating a new one, the result was the same. I include the header #include < QFileDialog >. Bellow you have my code posted.
QFileDialog *dialog;
dialog = new QFileDialog(this);
dialog->setFileMode(QFileDialog::AnyFile);
dialog->setNameFilter(tr("Images (*.png *.xpm *.jpg *.bmp)"));
QStringList l;
QString file;
if (dialog->exec())
{
l = dialog->selectedFiles();
file = l.at(0);
//do things with the file
QMessageBox::information(this,"Image Loader","Image loaded successful");
}
If anyone knows how to solve this I would be grateful.
I have a push button on my main window that allows the user to select a file to open.
std::fstream infile;
std::string filename = QFileDialog::getOpenFileName(this, tr("TXT file"), qApp->applicationDirPath (),tr("TXT File (*.txt)")).toStdString();
if (filename.empty())
return;
infile.open(filename, std::fstream:: in | std::fstream::out | std::fstream::app);
if (true) {
//Does stuff with the data
}
infile.close();
This normally works fine, and I've used it in previous gui qt applications. However, for this application the mainwindow (upon its setup) sets its windowsflags as follows:
setWindowFlags(Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint);
This creates a problem as the main window attempts to always stay on top (and thus prevents the open file window from appearing). Without the staysontop flag the file dialog works correctly.
Is there a way to temporarily disable this flag (so I can disable when the push button is clicked and then reenable when the file dialog is complete)?
setWindowFlags(Qt::FramelessWindowHint| ~Qt::WindowStaysOnTopHint);
This seems to be the most common suggested solution, but doesn't work for me. I believe this is because the window has to be recreated for the changes to the window flag to be registered -- however, I believe that if I killed the main window the file dialog would go out of scope too?
In summary, I am trying to find a work around to have the main window always on top except for when I am trying to select a file to open (the file dialog being triggered by a push button).
The problem was not the flags but rather a timer emitting signals which updated the window position:
QTimer* timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update_pos()));
timer->start(50);
Where the update_pos function is as follows:
void MainWindow::update_pos(){
RECT rect;
if (GetWindowRect(target_window, &rect)) {
SetWindowPos((HWND)this->winId(), HWND_TOPMOST, rect.left, rect.top, 0, 0, SWP_NOSIZE);
} else {
//maybe window was closed
qDebug() << "GetWindowRect failed";
QApplication::quit();
}
}
Whenever the update_pos function was called it would push the main window to the top (and thus above the open file dialog).
Whenever I select a file in my QFileDialog the accepted signal is fired and the window closes. I want to keep the window open so I can select multiple files and then capture the signal fired when "open" is clicked.
QFileDialog* myDialog = new QFileDialog(this);
myDialog->setFileMode(QFileDialog::ExistingFiles);
myDialog->setVisible(true);
What signals should I be connecting here to achieve this effect?
The QFileDialog::ExistingFiles should guarantee that multiple files can be selected. Given that, you can connect to the signal:
void QFileDialog::filesSelected(const QStringList & selected)
Directly from the documentation:
When the selection changes for local operations and the dialog is accepted, this signal is emitted with the (possibly empty) list of selected files.
However, if you are only interested in collecting such files, you can totally avoid signal-slot and write (taken again from the documentation):
QStringList fileNames;
if (dialog.exec())
fileNames = dialog.selectedFiles();
Note that in this case dialog object has been created on the stack (which is the common approach for such objects).
Your code looks fine to me. I believe you are double clicking on the file inside the dialog instead of holding on the Ctrl and single clicking on all the files you need.
You can optionally use an event filter and ignore the double click event.
Once you click on Open, you can get a list of all the file paths in the QStringList given by QFileDialog::selectedFiles(). Also it's better to use a stack variable here and use exec method to launch it as pointed out by BaCaRoZzo.
QFileDialog myDialog(this);
myDialog.setFileMode(QFileDialog::ExistingFiles);
if(myDialog.exec())
{
qDebug() << myDialog.selectedFiles();
}
Whenever I select a file in my QFileDialog the accepted signal is fired and the window closes. I want to keep the window open so I can select multiple files
All other answers is just solution for selection many files one time and CLOSE window after Open button pressing. Get my solution, it is not very simple because it required lot of work:
I used lamda expressions and new signals and slots syntax in my answer, but you can use old syntax or add
CONFIG += c++11
to the .pro file and use lambdas.
Subclass QFileDialog:
Header:
#ifndef CUSTOMFILEDIALOG_H
#define CUSTOMFILEDIALOG_H
#include <QFileDialog>
#include <QDebug>
class CustomFileDialog : public QFileDialog
{
Q_OBJECT
public:
explicit CustomFileDialog(QWidget *parent = 0);
void setDefaultGeo(QRect);
signals:
void newPathAvailable(QStringList list);
public slots:
private:
bool openClicked;
QRect geo;
};
#endif // CUSTOMFILEDIALOG_H
When you click open, you hide your dialog, not close! Cpp:
#include "customfiledialog.h"
CustomFileDialog::CustomFileDialog(QWidget *parent) :
QFileDialog(parent)
{
openClicked = false;
connect(this,&QFileDialog::accepted,[=]() {
openClicked = true;
qDebug() << openClicked;
this->setGeometry(geo);
this->show();
emit newPathAvailable(this->selectedFiles());
});
}
void CustomFileDialog::setDefaultGeo(QRect rect)
{
geo = rect;
}
Usage:
CustomFileDialog *dialog = new CustomFileDialog;
QStringList fileNames;
dialog->setFileMode(QFileDialog::ExistingFiles);
dialog->show();
dialog->setDefaultGeo(dialog->geometry());
connect(dialog,&CustomFileDialog::newPathAvailable,[=](QStringList path) {
qDebug() << path;
});
Why do you need setDefaultGeo? Without this method, your window will move after Open pressing.
What we get?
I open filedialog and select two files:
I clicked Open, but window didn't close! You can choose new files again and again!
One more file and so on:
Window will closed only when user press Close button, but you will have all path which user choose.
As you said:
I want to keep the window open so I can select multiple files
You get this.
I don't think anyone has understood the question (or it could be just me looking for my own solution)...
I had the same issue. As soon as I clicked a file the dialog would close. I couldn't ever select a file and then click "Open" because the dialog instantly closed as soon as I single clicked a file.
related: qtcentre.org/threads/48782-QFileDialog-single-click-only
It turns out it was my linux os settings (under mouse). File opening was set to single-click. I still feel like something external might have toggled this but that is just speculation. It appears Qt was going the right thing. Check another application, like kate on KDE and see if it has the same behavior. That is what clued me in to the source of my issue.
void OBJ_Loader::on_actionOpen_triggered()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open a File"));
if (!filename.isEmpty()) {
filepath=filename.toUtf8().constData();
command.append(filepath);
int TempNumOne=command.size();
for (int a=0;a<=TempNumOne;a++) { //get letters to a char list so it can be used by system();
cmd[a]=command[a];
}
openfile=true;
if (openfile) {
openfile=false;
system(cmd);
}
}
}
When the system(cmd); is called the QFileDialog window does not close till the system command finishes. I would like to know if I can close the search window after clicking open.
The system function blocks the event loop: the user interaction requires the event loop to run, and it runs when your code isn't running. Since the system invocation is in your code, you can't simply have it block your process. You need to use QProcess as it has an asynchronous interface. This answer provides a complete example of one process calling itself -- all done from a single executable.