QFileDialog problem [SOLVED]
-
Hi,
I've an application with a QWidget that have a my personalized QWidget to show images and multiple buttons, one of it open a QFileDialog to save a file, but it does not work properly it seems that does not render correctly.
The really strange things is that if I don't open an Image in the my qwidget it work correctly, but in the other case I have this QFileDialog:
http://postimg.org/image/pz3olh7qd/
or this:
http://postimg.org/image/semsddtft/
The code is:
void AnalyzerForm::on_buttonSavePatternParams_clicked()
{
QString filename = QFileDialog::getSaveFileName(this, tr("Save file"), "./"+selectedFile, tr("_pp.yml"));
filename = filename + "_pp.yml";
controller->savePatternParams(filename.toStdString());
}
And the code to open the image is:
void CVWidget::showImage(QImage image)
{
orig_image = QPixmap::fromImage(image);
m_imageLabel->setPixmap(orig_image);
}
I tried to commented out the line with the setPixmap instruction, and the QFileDialog works.
What could be the problem between this two functions?
Sorry for my english. -
@annettaws Some questions that come to mind:
- which Qt version?
- How are the two pieces of code related? I don't see any call of
showImage
in the first part orig_image
is not declared/defined anywhere, and doesn't look to be a member - it seems like you tried to create some minimal example, but you striped away way too much. I don't think many are capable to help you it based on this (unless someone happens to have seen the same effect and remembers why)
So maybe, expand a bit on your example, and make it into a compiling example if you can.
-
@Jakob:
I'm using the Qt 5.4.2.
I've a QWidget and the myQWidget to show the images is a member of hisui
( I added graphicaly a widget and promoted to my class).
Below there are all the code of the two Qwidges:
CVWidget is the Widget to show Images
CVWidget::CVWidget(QWidget *parent) : QWidget(parent)
{m_layout = new QVBoxLayout; m_imageLabel = new QLabel; m_layout->addWidget(m_imageLabel); setLayout(m_layout); startSelection = false; corner1 = QPoint(); corner2 = QPoint(); startRadius = false; startSearchArea = false;
}
CVWidget::~CVWidget(){}
void CVWidget::setAnalyzerForm(AnalyzerForm * analyzerForm)
{_analyzerForm = analyzerForm; form = 'a';
}
void CVWidget::setTrainerForm(TrainerForm * trainerForm)
{_trainerForm = trainerForm; form = 't';
}
void CVWidget::setController(ControllerQt * controller)
{contr = controller;
}
void CVWidget::showImage(QImage image)
{orig_image = QPixmap::fromImage(image); m_imageLabel->setPixmap(orig_image);
}
QImage CVWidget::getCurrentImageShowed()
{return m_imageLabel->pixmap()->toImage();
}
void CVWidget::mousePressEvent(QMouseEvent *mouseEvent)
{this->setCursor(Qt::CrossCursor); if( form == 'a') { if (_analyzerForm->radioBSelectionBoxIsSelected()) { startSelection = true; corner1 = mouseEvent->localPos().toPoint(); _analyzerForm->showStatusBarMessage(QString("To add the Selction Box press 'a'. To store all press the button."), QString("red")); } if (_analyzerForm->radioBRadiusIsSelected()) { startRadius = true; corner1 = mouseEvent->localPos().toPoint(); _analyzerForm->showStatusBarMessage(QString("Save current MSD-PR press 'a'.After the Detection press 's' to add msd params."), QString("red")); } if (_analyzerForm->radioBSearchAreaIsSelected()) { startSearchArea = true; corner1 = mouseEvent->localPos().toPoint(); _analyzerForm->showStatusBarMessage(QString("Save the current MSD-SAR press 'a'.After the Detection press 's' to save msd params."), QString("red")); } }
}
void CVWidget::mouseMoveEvent(QMouseEvent *mouseEvent)
{if(form == 'a') { if(startSelection && _analyzerForm->radioBSelectionBoxIsSelected()) { corner2 = mouseEvent->localPos().toPoint(); this->repaint(); } if (startRadius && _analyzerForm->radioBRadiusIsSelected()) { corner2 = mouseEvent->localPos().toPoint(); this->repaint(); } if (startSearchArea && _analyzerForm->radioBSearchAreaIsSelected()) { corner2 = mouseEvent->localPos().toPoint(); this->repaint(); } } else if (form == 't') { this->setCursor(Qt::CrossCursor); QPoint point = QPoint(mouseEvent->localPos().toPoint().x(),mouseEvent->localPos().toPoint().y()+10); _trainerForm->showStatusBarMessage(QString("Mouse move (%1,%2)").arg(point.x()).arg(point.y()), "gray"); }
}
void CVWidget::mouseReleaseEvent(QMouseEvent * mouseEvent)
{if( form == 'a') { this->setCursor(Qt::ArrowCursor); if(startSelection && _analyzerForm->radioBSelectionBoxIsSelected()) { contr->addSelection(corner1.x(),corner1.y(), corner2.x(),corner2.y()); startSelection=false; } if (startRadius && _analyzerForm->radioBRadiusIsSelected()) { contr->setCurrentMSDPatchRadius(corner1.x(), corner1.y(), corner2.x(), corner2.y()); startRadius = false; } if (startSearchArea && _analyzerForm->radioBSearchAreaIsSelected()) { contr->setCurrentMSDSearchArea(corner1.x(), corner1.y(), corner2.x(), corner2.y()); startSearchArea = false; } } else if(form == 't') { this->setCursor(Qt::CrossCursor); QPoint posWidg = this->pos(); QPoint point = QPoint(mouseEvent->localPos().toPoint().x()-(posWidg.x()), mouseEvent->localPos().toPoint().y()); _trainerForm->showStatusBarMessage(QString("Mouse clicked (%1,%2)").arg(point.x()).arg(point.y()), "gray"); contr->changeLabel2KP(point); contr->setCurrentImgToShow(contr->getCurrentImageWithKP()); showImage(contr->getCurrentImgToShow()); }
}
void CVWidget::paintEvent(QPaintEvent * )
{int w = orig_image.width(); int h = orig_image.height(); if( w <= 0 || h <= 0) return; QImage::Format f = orig_image.toImage().format(); QImage image (w,h,f); QPainter painter(&image); if(startSelection) { painter.setPen(QPen(Qt::red, 2)); } else if (startRadius) { painter.setPen(QPen(Qt::cyan, 2)); } else if (startSearchArea) { painter.setPen(QPen(Qt::magenta, 2)); } QRectF rectangle(corner1,corner2); painter.drawPixmap(0,0,orig_image); painter.drawRect(rectangle); painter.end(); m_imageLabel->setPixmap(QPixmap::fromImage(image));
}
The AnalyzerForm is the QWidget opened in a QMainWindow, and on it I have the problem.
AnalyzerForm::AnalyzerForm(QWidget *parent) :
QWidget(parent), ui(new Ui::AnalyzerForm), controller(new ControllerQt()), fileSystemModel(new QFileSystemModel())
{
ui->setupUi(this); fileSystemModel->setRootPath(PATH); ui->treeViewFileSystem->setModel(fileSystemModel); ui->treeViewFileSystem->setRootIndex(fileSystemModel->index(PATH)); ui->myCVWidget->setAnalyzerForm(this); ui->myCVWidget->setController(controller); ui->radioBSelectionBox->setChecked(true); //selection trigger a slot QItemSelectionModel * selectionModel = ui->treeViewFileSystem->selectionModel(); connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)), this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)));
}
AnalyzerForm::~AnalyzerForm()
{delete ui; delete controller;
}
void AnalyzerForm::setStatusBar(QStatusBar * statusBar)
{_statusBar = statusBar;
}void AnalyzerForm::showStatusBarMessage(QString message, QString color)
{_statusBar->showMessage(message); _statusBar->setStyleSheet(QString("QLabel { background-color : %1; color : black; }") .arg(color));
}
bool AnalyzerForm::radioBRadiusIsSelected()
{return ui->radioBPatchRadius->isChecked();
}
bool AnalyzerForm::radioBSearchAreaIsSelected()
{return ui->radioBSearchArea->isChecked();
}bool AnalyzerForm::radioBSelectionBoxIsSelected()
{return ui->radioBSelectionBox->isChecked();
}void AnalyzerForm::on_buttonOpenImg_clicked()
{if(!selectedFile.isEmpty()) { //set current image showed controller->openCurrentImage(selectedFile.toStdString()); QImage imageShowed = controller->getCurrentImage(); if(imageShowed.isNull()) { QMessageBox::information(this,"Image Viewer","Error Displaying image"); return; } ui->myCVWidget->showImage(imageShowed); ui->radioBPatchRadius->setEnabled(true); ui->radioBSearchArea->setEnabled(true); } return;
}
void AnalyzerForm::selectionChangedSlot(const QItemSelection & , const QItemSelection & )
{//get the text of the selected item const QModelIndex index = ui->treeViewFileSystem->selectionModel()->currentIndex(); selectedFile = fileSystemModel->filePath(index); return;
}
void AnalyzerForm::on_buttonSave_clicked()
{controller->saveSelections();
}
void AnalyzerForm::on_buttonSavePatternParams_clicked()
{QString filename = QFileDialog::getSaveFileName(this->parentWidget(), tr("Save file"), "./"+selectedFile, tr("_pp.yml")); filename = filename + "_pp.yml"; controller->savePatternParams(filename.toStdString());
}
void AnalyzerForm::on_buttonDetectKpAll_clicked()
{controller->detectAll(); ui->myCVWidget->showImage(controller->getCurrentImageWithKP()); showStatusBarMessage(QString("Detected: keyPoints. "), QString("green"));
}
void AnalyzerForm::on_buttonDetectKpCurrent_clicked()
{controller->detectCurrent(); ui->myCVWidget->showImage(controller->getCurrentImageWithKP()); showStatusBarMessage(QString("Detected: keyPoints. Radius %1 SearchArea %2 Th. Saliency AVG %3") .arg(controller->getCurrentMsdPatchRadius()) .arg(controller->getCurrentMsdSearchAreaRadius()) .arg(controller->getCurrentMsdThSaliency()), QString("green"));
}
void AnalyzerForm::keyPressEvent(QKeyEvent * event)
{if (event->key() == Qt::Key_C) { ui->radioBPatchRadius->setEnabled(true); ui->radioBSearchArea->setEnabled(true); controller->setCurrentImgToShow(controller->getCurrentImage()); ui->myCVWidget->showImage(controller->getCurrentImgToShow()); showStatusBarMessage(QString("Original Image."), QString("white")); } if (event->key() == Qt::Key_A && radioBRadiusIsSelected()) { ui->radioBPatchRadius->setEnabled(false); controller->setCurrentImgToShow(ui->myCVWidget->getCurrentImageShowed()); ui->myCVWidget->showImage(controller->getCurrentImgToShow()); showStatusBarMessage(QString("Current MSD Radius saved."), QString("yellow")); } if (event->key() == Qt::Key_A && radioBSearchAreaIsSelected()) { ui->radioBSearchArea->setEnabled(false); controller->setCurrentImgToShow(ui->myCVWidget->getCurrentImageShowed()); ui->myCVWidget->showImage(controller->getCurrentImgToShow()); showStatusBarMessage(QString("Current MSD Search Area saved."), QString("yellow")); } if (event->key() == Qt::Key_A && radioBSelectionBoxIsSelected()) { controller->setCurrentImgToShow(ui->myCVWidget->getCurrentImageShowed()); ui->myCVWidget->showImage(controller->getCurrentImgToShow()); showStatusBarMessage(QString("Selection Box added."), QString("yellow")); } if (event->key() == Qt::Key_S ) { controller->addMsdParameters(controller->getCurrentMsdPatchRadius(), controller->getCurrentMsdSearchAreaRadius(), controller->getCurrentMsdThSaliency()); showStatusBarMessage(QString("Current MSD Parameters added to list. Press the button to store it."), QString("green")); }
}
I've the problem when I press the button OpenFile ( that call the function
void AnalyzerForm::on_buttonOpenImg_clicked()
) and then when I press the button Save Pattern Params ( the functionsvoid AnalyzerForm::on_buttonSavePatternParams_clicked()
).
If I just press the button Save Pattern Params (so without opening any images )I don't have any problem. I think that there is an "interference" beteween the two QWidget.Before the my application was structured with a QMainWindow with some buttons that will open a different QWidgets in different Window, for example the main window and then the "Analyzer Form" window, and everything was working.
Now I have a QMainWindow with a menuBar and with the functionsetCentralWidget
I assign the different Qwidget. Reading the code of the QMainWindow maybe is more explanatory:
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this); //Default start with the AnalyzerForm AnalyzerForm * form = new AnalyzerForm(this); form->setStatusBar(ui->statusbar); setCentralWidget(form); setWindowTitle("SVM Classifier Application - Analyzer"); // adjustSize();
}
MainWindow::~MainWindow()
{delete ui;
}
void MainWindow::closeEvent (QCloseEvent *){}
void MainWindow::on_actionOpen_triggered()
{AnalyzerForm * form = new AnalyzerForm(this); form->setStatusBar(ui->statusbar); setCentralWidget(form); setWindowTitle("SVM Classifier Application - Analyzer Msd Parameters");
// adjustSize();
}void MainWindow::on_actionTrainer_triggered()
{TrainerForm * form = new TrainerForm(this); form->setStatusBar(ui->statusbar); setCentralWidget(form); setWindowTitle("SVM Classifier Application - Trainer");
// adjustSize();
}void MainWindow::on_actionClassifier_triggered()
{ClassifierForm * form = new ClassifierForm(this); form->setStatusBar(ui->statusbar); setCentralWidget(form); setWindowTitle("SVM Classifier Application - Classifier");
// adjustSize();
}void MainWindow::on_actionRoc_Curve_triggered()
{PlotForm * form = new PlotForm(this); setCentralWidget(form); setWindowTitle("SVM Classifier Application - Curve Roc Analyzer");
}
Could be this different structure of the Qwidget and QMainWindow an other effect of my problem?