QtCreator GUI open text file - qt

I made a push button that will browse and get a textfile. But I need to open it in a new window to check if the content of the textfile is correct. How do I do this?
Also, I would like to have a line edit right next to the button that shows which file I'm looking at. In other words, the directory of the file that is opened through the button.
Currently, this is what I have:
void MainWindow::on_fileButton_clicked()
{
QString fileName1 = QFileDialog::getOpenFileName(this,tr("Open Text File"), "", tr("Text Files (*.txt)"));
QFile file1(fileName1);
if(!file1.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file1);
while(!in.atEnd()){
QString line = in.readLine();
}
}

I suggest using one of powerful text interfaces available:
void MainWindow::openfile() {
QString fileName1 = QFileDialog::getOpenFileName(this,tr("Open Text File"), "", tr("Text Files (*.txt)"));
QFile file1(fileName1);
if(!file1.open(QIODevice::ReadOnly | QIODevice::Text))
return;
// show the directory path of opened file
dir->setText(QFileInfo(file1).dir().path());
QTextBrowser *b = new QTextBrowser;
b->setText(file1.readAll());
b->show();
}
dir is a member variable, initialized in constructor with
dir = new QLineEdit(this);

you should make a new window by add a Dialog or Mainwindow. after that add widgets like textEdit and other things to your new Dialog.
you need to learn some basics of Qt framework:
there is very well documentation of Qt, you can use it.
and also there is about 100 short videos of Qt learning.

Related

Qt. Problem with the QFileDialog: setDirectory() and directory()

I have a problem with the QFileDialog class, namely with the setDirectory() and directory() methods. I need to make it so that after opening a file, my program remembers the directory in which the selected file is stored, and the next time QFileDialog is called, it automatically opens the directory that was used last. Here is a snippet of my code:
static QString _st_doc_last_directory;
void MainWindow::open()
{
if (!fileDialog)
{ fileDialog = new QFileDialog(this);
}
if (!_st_doc_last_directory.isEmpty()) fileDialog->setDirectory(_st_doc_last_directory);
QString fileName = fileDialog->getOpenFileName(this, tr("Open Document"), ".", tr("Compressed CAD Models (*.data)"));
if (!fileName.isEmpty())
{ _st_doc_last_directory = fileDialog->directory().dirName();
}
}
The crux of my problem is that when the setDirectory() or directory() method is called, my program crashes with a
"Segmentation fault"
message. How can I fix it, please advise. Thanks in advance.
Whenever you start this method, you have this as the start window: ".". (admittedly I don't know what's going on internally, but I think this leads to this problem).
You can query beforehand whether your defined string is empty. if so you set a path, otherwise you store one in your string. If you don't want to do this from the beginning every time you start the program, you can also use QSettings. This saves you the path in the registry (ie if you use windows).
With QFileInfo you can easily get the path
void MainWindow::open()
{
if(_st_doc_last_directory.isEmpty())
_st_doc_last_directory = QDir::homePath();
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Document"), _st_doc_last_directory, tr("Compressed CAD Models (*.data)"));
QFileInfo info(fileName);
if(!fileName.isEmpty())
_st_doc_last_directory = info.absolutePath();
}

How to set a default file name and a current directory in getSaveFileName dialog at the same time?

I read in this Qt reference http://doc.qt.io/qt-5/qfiledialog.html#getSaveFileName about getSaveFileName() function in qFileDialog. In the third parameter const QString & dir = QString(), I can provide with a current directory or a default file name, but I don't know how to provide both currentPath and ui->titleEdit->text().trimmed() in getSaveFileName() function at the same time.
void TipManager::on_saveButton_clicked()
{
//save dialog
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save Tip Code"), currentPath /*ui->titleEdit->text().trimmed()*/,
tr("Tip Code (*.txt);;All Files (*)"));
saveFile(fileName);
}
currentPath is a current directory and ui->titleEdit->text().trimmed() is a default file name when I open a save dialog box.
How could I solve this?
#edit (solved)
I solved this question with a help of #thuga :
void TipManager::on_saveButton_clicked()
{
//save dialog
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save Tip Code"), currentPath +"/"+ ui->titleEdit->text().trimmed(),
tr("Tip Code (*.txt);;All Files (*)"));
saveFile(fileName);
}
I combined two QString variables with + and it seems that it is separated by /, so I put it in between two variables.

Why QFormBuilder save BAD *ui files?

Note: The question from http://www.qtcentre.org/threads/31327-QFormBuilder-save-BAD-
I create in QtDesigner standart QWidget and add standart widgets (QPushButtons, QLineEdit and etc.)
Then save it to "formIn.ui" file. Next, load and save it with QFormBuilder in "formOut.ui" BUT size of dest file ("formOut.ui") more then 126kb!!!!
Scr file "formIn.ui" created with QtDesigner is 2kb only!!! Also then I open "formOut.ui" in QtDesigner it's opened incorrect!
It's trouble me! May be some people know how to solve it?
QFormBuilder fb;
QFile fileIn("c:/formIn.ui"); //file created in Qtdesigner
QFile fileOut("c:/formOut.ui");
bool isOk =false;
isOk = fileIn.open(QIODevice::ReadOnly);
isOk = fileOut.open(QIODevice::ReadWrite);
QWidget *w = fb.load(&fileIn, 0);
fb.save(&fileOut, w);

view an image in qt for symbian

I have an application that I implemented, and want to display an image in a widget, but I get an error SIGSEGV, I view it from a directory, but in reality after heave from a buffer
not what the problem is I leave my code here:
void Image:: on_pushButton_clicked ()
{
this-> conn-> connect (this-> ui-> lineEdit_2-> text (),
this-> ui-> lineEdit-> text (). Toint ()); QLayout* layout;
QString fileName = QFileDialog:: GetOpenFileName (this,
tr ("Open File"), QDir:: currentPath (),
"files (*. jpg *. png)");
QImage image (fileName);
QLabel * label = new QLabel (this);
label-> setPixmap (QPixmap:: FromImage (image));
label-> setScaledContents (true);
layout-> addWidget (label);
label-> show ();
}
I can leave if you want to help my application link for the download
thank you very much
thank you very much and I managed to see the image, I want to load from a buffer like this: QBuffer * buffer = new QBuffer (conex);
QImage * image = new QImage ();
image-> loadFromData (buffer-> buffer ());
I tried this to make my image but I get errors
QBuffer * buffer = new QBuffer (conn);
QImage image = new QImage ();
image-> loadFromData (buffer-> buffer ());
errors are as follows:
C:\ejemplos_qt\teratermobile-build-simulator..\teratermobile\imagen.cpp:81: error: conversion from 'QImage*' to non-scalar type 'QImage' requested
C:\ejemplos_qt\teratermobile-build-simulator..\teratermobile\imagen.cpp:82: error: base operand of '->' has non-pointer type 'QImage'
You didn't initialize your layout variable before using it with something like this:
layout = new QVBoxLayout(widget); // widget is where you want to put the QLabel
or you can reuse an already created layout.
If you want to show the QLabel as a top-level window, remove completely the two lines about the layout.

Qt: opening qrc pdf with the poppler library

I'm having a bit of trouble with my function for displaying pdf's with the poppler library. The code below is the function in which the problem occurs.
const QString &file is the path to the file
int page is the page on which it has to open
When i set file to a real path (e.g. "/Users/User/Documents/xxx.pdf"), it is no problem to open it. But when i give the path to a qrc file (":/files/xxx.pdf"), it won't work. I want to use it for displaying a user manual for instance, within the application.
I've also tried first making a QFile out of it, opening it and doing readAll, then loading the QByteArray received by doingPoppler::Document::loadFromData(the qbytearray), but it errors already when opening the QFile in ReadOnly mode.
void class::setPdf(const QString &file, int page)
{
Poppler::Document *doc = Poppler::Document::load(file);
if (!doc) {
QMessageBox msgbox(QMessageBox::Critical, tr("Open Error"), tr("Please check preferences: cannot open:\n") + file,
QMessageBox::Ok, this);
msgbox.exec();
}
else{ /*Code for displaying the pdf, which works fine*/
}
}
I hope you can help me,
greetings,
Matt
I've also tried first making a QFile
out of it, opening it and doing
readAll, then loading the QByteArray
received by
doingPoppler::Document::loadFromData(the
qbytearray), but it errors already
when opening the QFile in ReadOnly
mode.
QFile f;
f.setFileName(":/skin/AppIcon16.png");
f.open(QIODevice::ReadOnly);
QByteArray r=f.readAll();
Perfectly reads all data from the resource, have checked it. So i suggest you did something wrong when tried that. Maybe path errors, maybe something else...

Resources