Loading all the Images from the file Qt - qt

I want to load all the images from the text file to widget in Qt.
I have written the code in this way,
void project::showfile()
{
QString fileName = "/home/main/Desktop/image_file.txt";
show_image(fileName);
}
void project :: show_image(const QString &fileName)
{
Widget *wid1=new Widget(ui->scrollArea_2);
QFile file(fileName);
qDebug()<<"test";
if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
{
qDebug() << "Error opening file";
}
QString line;
QTextStream InputDataFile(&file);
while(!InputDataFile.atEnd())
{
line = InputDataFile.readLine();
QPixmap pixmap(line);
wid1->setPixmap(line);
wid1->resize(pixmap.size());
wid1->show();
}
file.close();
}
For your example my image_file.txt contains
/home/keerthana/Desktop/images/2_222_31.jpg
While I am using this code I can load only the last image in the file. I want to load all the images to the GUI.
Can any one help me? That how can I load everything.

The code is not complete, but the code seems to suggest you're loading the image repeatedly on the same widget. It seems correct that only the last is shown. Each pixmap you load seems to replace the previous...

Related

QMainWindow goes background after using QFileDialog

I have a Qt application that loads some file after clicking on button. I am using QFileDialog to open files. Every time I close file dialog, the application main window (QMainWindow) is behind all other opened windows (internet browser, explorer etc.). Is this standard behaviour? And is there any way to prevent this? I tried
// set always on top window
Qt::WindowFlags flags = this->windowFlags();
this->setWindowFlags(flags | Qt::WindowStaysOnTopHint);
but it causes that also file dialog is behind main window.
Edited:
Here is fragment of code to call file dialog (method that calls it is static method of my class LoadData):
void MainWindow::on_buttonIP_clicked()
{
loaded = LoadData::OpenFiles(IPFiles);
}
bool LoadData::openFiles(QStringList &fileNames)
{
// open files dialog
fileNames = QFileDialog::getOpenFileNames(0,
tr("Open files"), "", "Text Files (*.txt *.dat *.points)");
if (fileNames.isEmpty())
{
return false;
}
return true;
}
First argument of QFileDialog::getOpenFileNames must not be nullptr. Set it to your main window, and everything will be OK.

How to associate the selected item of Combobox to write into a file

Basically I have a line-edit box which take the user input such as comma separated values and on clicking the push-button it write all the values of line-edit box to a text file. But same feature I want to achieve through combobox. So whenever one of the item selected from combobox it should write the contents of line-edit box to a text file.
The code I have used so far to implement using push-button
void MainWindow::writefile()
{
QString str = ui->lineEdit->text();
QString filename = "data.txt";
QFile file(filename);
if(file.open(QIODevice::WriteOnly|QIODevice::Text))
{
QTextStream out(&file);
out<<str<<endl;
file.close();
}
}
void MainWindow::on_pushButton_clicked()
{
writefile();
}
Sounds like all you need to do is implement ItemListener and make itemStateChanged do exactly what your actionPerformed does.

cutycapt does not create links inside the pdf

I am using CutyCapt to create pdf from html, the issue is that the pdf does not contain links.
it removes all the links from the pdf. now I saw in that code that it creates object from type
QWebFrame and then print that frame to the printer.
QWebFrame *mainFrame = mPage->mainFrame();
QPainter painter;
case PdfFormat:
case PsFormat: {
QPrinter printer;
printer.setPageSize(QPrinter::A4);
printer.setOutputFileName(mOutput);
// TODO: change quality here?
mainFrame->print(&printer);
break;
}
Any idea, what I need to do on the QT side to enable links .

How to print image file from a printer using QT

I am trying to print an image file on printer using QWebview but instead of image blank page is printed. Please find the below code.
void ChartViewer::onprintBtnClicked()
{
QString fileName = QFileDialog::getOpenFileName(this,"Open File",QString(),"Pdf File(*.png)");
qDebug()<<"Print file name is "<<fileName;
if(fileName.endsWith(".png"))
{
QPrinter printer;
QWebView *view = new QWebView;
QPrintDialog *dlg = new QPrintDialog(&printer,this);
printer.setOutputFileName(fileName);
if(dlg->exec() != QDialog::Accepted)
return;
view->load(fileName);
view->print(&printer);
}
}
If I use view->show() then it has shown the image properly but printed page is coming blank. Request you to please look into the above code and correct me where I am doing wrong.
Regards,
Lekhraj
You load some png file into fileName. Then you set QPrinter to print to that png file with printer.setOutputFileName(fileName);.I suppose it is wrong, it should be some different pdf file probably.
I'm not sure if I understand what are you trying to do? How to print image file using QPrinter? Into pdf file? Why are trying to use QWebView?
You can use QImage to load image file and then paint with QPainter on QPrinter.
#include <QtGui>
#include <QtCore>
int main(int argc, char** argv) {
QApplication app(argc, argv);
QString fileName = QFileDialog::getOpenFileName(0,"Open File",QString(),"PNG File(*.png)");
QPrinter printer;
QPrintDialog *dlg = new QPrintDialog(&printer,0);
if(dlg->exec() == QDialog::Accepted) {
QImage img(fileName);
QPainter painter(&printer);
painter.drawImage(QPoint(0,0),img);
painter.end();
}
delete dlg;
QTimer::singleShot(1, &app, SLOT(quit()));
app.exec();
return 0;
}
Some of your issues may overlap with your other question
https://stackoverflow.com/questions/8297239/how-to-print-pdf-file-in-qt
You try to print the QWebView immediately after you call its load() function. But the QWebView has not yet loaded the content and the view is therefore blank. You need to connect the QWebView's loadFinished signal to some slot where you can call the print() function. Read the QWebView's documentation.

Quiloader not loading ui file

I am trying a simple code to load a ui file runtime, but not able to load it.
QUiLoader loader;
QFile file(":/dialog.ui");
file.open(QFile::ReadOnly);
QWidget *myWidget = loader.load(&file, this);
file.close();
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(myWidget);
setLayout(layout);
I have added the dialog.ui file in my .qrc file as well as mentioned here . Not getting any error message. Please tell me what has gone wrong.
Regards
Ok got it.. Here's the code.
QUiLoader uiLoader;
QFile file(":/dialog.ui");
QWidget *sortDialog = uiLoader.load(&file);
if (sortDialog) {
sortDialog->show();
}

Resources