How to split large style file (qss file) in QT to small file and load them all - qt

What I am trying to do I need to split my large style file to small style files
because now it is hard to read the style file(qss file) and add new styling to it.
after that i need to load those small qss files to apply them all
I am loading my Big file by calling function on main I have created
void Utilities::loadEnglishStyle()
{
QFile file(":/EnglishClasses.qss");
file.open(QFile::ReadOnly);
QString StyleSheet = QLatin1String(file.readAll());
qApp->setStyleSheet(StyleSheet);
file.close();
}
I thought about split the big file, add the small files to the resources, open all of them by QFile and after that concatenate them all in one string
but every time when adding new qss file i still need to do the same process again
Is there any efficient way to do this ?!!

Your approach sounds correct. The process is not as complex as it sounds. The only thing you need is to add the "smaller" qss file in the resources, under a specific prefix (eg. stylesheets), and then automatically load and concatenate all these files. Sample code follows:
QDir stylesheetsDir(":/stylesheets");
QFileInfoList entries = stylesheetsDir.entryInfoList();
QString completeStylesheet = "";
foreach (QFileInfo fileInfo, entries)
{
QFile file(":/stylesheets/" + fileInfo.fileName(););
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
continue;
QTextStream in(&file);
completeStylesheet += in.readAll()
}

you means some API shoud add as:
QWidget::addStyleSheet(StyleSheet);
QWidget::clearStyleSheet(StyleSheet);

Related

How to can append the value from txt file to the Qlist

How I can to insert the value from txt file in to the Qlist...
QList<QString> list_StRead;
list_StRead.insert();
I can sorting txt file ... its mean that my file is a line by line. than after the insert to the Qlist I want to write in to Qtabelewidget. How I must to do?? u must to be completely understand. see the img file
tnx for all....
Here is the pseudo code.
Please try it. (Not tested, code written in notepad. excuse me any syntax errors).
//Your list
QList<QString> list_StRead;
//Text stream object read data and insert in list.
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine(); //read one line at a time
list_StRead.push_back(line);
}
//loop your list
for(int i =0; i<list_StRead.size(); i++)
{
//Add by create your tablewidget item and append it.
YourTableWidget->setItem(rowNumber,colNumber,new QTableWidgetItem(list_StRead.at(i)))
}

QTextStream atEnd() is returning true when starting to read from a file

I want to read and parse contents of the /proc/PID/status file on a linux machine, but the QTextStream.atEnd is always returning true when starting to read. The code:
QString procDirectory = "/proc/";
procDirectory.append(QString::number(PID));
procDirectory.append("/status");
QFile inputFile(procDirectory);
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
QString line;
while (!in.atEnd())
{
line = in.readLine();
File exists and if I read lines manually without the while expression, the files are read normally.
Did I miss something obvious?
(Debian 8 x64, QT 5.4.1 x64, gcc 4.9.2)
Nevermind found out I needed to read one line before the while clause, now it works.
The preferred way oft looping over these streams is with a do/while loop. This is for allowing the stream to detect Unicode correctly before any queries (like atEnd) are made.
QTextStream stream(stdin);
QString line;
do {
line = stream.readLine();
} while (!line.isNull());

check if a file extension is part of a given list

I am trying to make a simple widget which contains a lineedit which shows the file name and a button to open a filedialog.
and now I want to check if the file-extension is valid, in this case, a image file ending with jpg, png or bmp. I solved this with QFileInfo and QList, this code is in my btn_clicked slot:
QString filename = QFileDialog::getOpenFileName(this, tr("Select an image File", "", tr("Image Files (*.bmp *.jpg *.png);; All Files(*)"));
QList<QString> ext_list;
ext_list<<"bmp"<<"jpg"<<"png";
QFileInfo fi(filename);
QString ext = fi.suffix();
if (ext_list.contains(ext)){
// lineedit->setText(filename);
}
else {
QMessageBox msgBox;
msgBox.critical(0, "Error", "You must select a valid image file");
it works, but is there a more simple/elegant way to achieve the goal? Thx for your help.
You might be interested by the setNameFilters function : http://doc.qt.io/qt-5/qfiledialog.html#setNameFilters
Update
If you want to filter images without naming each extensions, you should use QMimeDatabase. This will allow you to define your filter for the QFileDialog and then get a list of extensions to check.
For example with jpeg and png:
QStringList mimeTypeFilters;
mimeTypeFilters << "image/jpeg" << "image/png";
QFileDialog fd;
fd.setFileMode(QFileDialog::ExistingFile);
fd.setMimeTypeFilters(mimeTypeFilters);
fd.exec();
QString filename = fd.selectedFiles().count() == 1 ? fd.selectedFiles().at(0) : "";
QMimeDatabase mimedb;
if(!mimeTypeFilters.contains(mimedb.mimeTypeForFile(filename).name()))
{
// something is wrong here !
}

Overwrite text file vs append

I'm looking to overwrite data in a text file but all I can seem to do is append to it
mFile.open(QFile::ReadWrite)
QTextStream in(&mFile);
QString first = in.readLine(); //discard the headers
QString dataLine = in.readLine(); //headers
QStringList sql_row = dataLine.split("\t"); //first row (sake of proj only 1 row)
if(sql_row[1].isEmpty()) //no user name registered
{
QByteArray user= getenv("USERNAME"); //for windows
if(user.isEmpty())
{
user = getenv("USER"); ///for MAc or Linux
}
dataLine = dataLine.insert(dataLine.indexOf("\t")+ 1,user);
in << first << endl << dataLine << endl;
mFile.flush();
mFile.close();
Change
mFile.open(QFile::ReadWrite);
to
mFile.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text);
The QIODevice vs QFile distinction isn't necessary, but I personally favor using the base class. The Truncate flag will overwrite (i.e., delete) an existing file.
Alternatively, you can follow the other suggestion and open your text file directly using one of QTextStream's constructors. The same QIODevice::OpenMode conventions apply. This only works if mFile is a FILE object and not a QFile, which isn't the case in your example.
A couple additional notes for beginners.
Related Note 1
You didn't ask about this, but I also added the QIODevice::Text flag to ensure that newline characters get translated to/from the local encoding (plain \n vs. \r\n) when you use endl.
A really common mistake is to use \r\n AND QIODevice::Text, which results in text files with double-returns \r\r\n on Windows. Just use QIODevice::Text when opening and simply \n or endl and you'll never have this problem.
Related Note 2
Using QTextStream::endl will automatically call flush() each time. If your loop is large, use "\n" instead to prevent a slowdown unless you actually need to flush every line. The stream will automatically write to disk as its buffer gets full, or when it's closed.
QFile::close() also calls flush(), which makes your mFile.flush() at the end redundant.
Use an overloaded constructor of QTextStream:
QTextStream in(&mFile, QIODevice::ReadWrite | QIODevice::Truncate);
The QIODevice::Truncate will remove all the previous content of the file, and QIODevice::ReadWrite will open it for read and write access.

read a text file to QStringList

I have a text file. I need to read it to a QStringList. there are no line seperators. I mean each line in the text file is in a new line. So is there anyway i can do this?
I assume that every line should be a separate string in the list. Use QTextStream::readLine() in a cycle and on each step append the returned value to the QStringList. Like this:
QStringList stringList;
QFile textFile;
//... (open the file for reading, etc.)
QTextStream textStream(&textFile);
while (true)
{
QString line = textStream.readLine();
if (line.isNull())
break;
else
stringList.append(line);
}
QFile TextFile;
//Open file for reading
QStringList SL;
while(!TextFile.atEnd())
SL.append(TextFile.readLine());
If the file isn't too big, read the whole content into a QString and then split() it into a QStringList.
I like using the QRegExp version to handle linefeed from different platforms:
QStringList sList = s.split(QRegExp("(\\r\\n)|(\\n\\r)|\\r|\\n"), QString::SkipEmptyParts);
I like my code to be fully indented/paranthesized with obvious variable names (they may take longer to type but are much easier to debug) so would do the following (but changing "myTextFile" and "myStringList" to more sensible names, such as "employeeListTextFile")
QFile myTextFile;
QStringList myStringList;
if (!myTextFile.open(QIODevice::ReadOnly))
{
QMessageBox::information(0, "Error opening file", myTextFile.errorString());
}
else
{
while(!myTextFile.atEnd())
{
myStringList.append(myTextFile.readLine());
}
myTextFile.close();
}
The below code reads the file
QFile File("/file_path");
if(!File.open(QIODevice::ReadOnly));
{
qDebug("Error");
}
QTextStream in(&File);
while(!in.atEnd())
{
qDebug()<<ReadAll;
ReadAll=in.readAll();
}
File.close();
Now the file is closed, now split the new line i.e \n here \r is carriage return
List= ReadAll.split(QRegExp("[\r\n]"),QString::SkipEmptyParts);

Resources