How to use Qt to read a file and display it quickly? - qt

I have to use Qt show the context of a file, whose size is 70M, but is so slow that it takes several minutes to display.
QFile file("farey.txt");
file.open(QFile::ReadOnly | QFile::Text);
QTextStream ReadFile(&file);
while (!ReadFile.atEnd()) {
QString line = ReadFile.readLine();
ui->Output->append(line);
}
file.close();
Output is TextEdit, can anyone give me some help to make it faster?
Can I use Qt to dispatch a default system editor to open the file??

If you want to display your file as plain text, the widget QPlainTextEdit is better then QTextEdit. It is optimized to handle large documents, for example see QTextEdit vs QPlainTextEdit
QTextEdit is an advanced WYSIWYG viewer/editor supporting rich text
formatting using HTML-style tags.
QTextEdit can display images, lists and tables.
QPlainTextEdit is an advanced viewer/editor supporting plain text.
QPlainText uses very much the same technology and concepts as
QTextEdit, but is optimized for plain text handling.
It is possible to open a file by default system file handler using QDesktopServices, for example:
QDesktopServices::openUrl(QUrl::fromLocalFile("file_path"));

First of all, this is enough:
QFile file("farey.txt");
file.open(QFile::ReadOnly | QFile::Text);
ui->Output->setPlaintText(file.readAll());
Second one - best optimization in your case it's logic optimization. Did you really need to show all of this 70M file?

Your problem is likely the appending the contents of the file line-by-line. This forces the document to be laid out repeatedly on each line - that's the cause of the slowdown. Read the entire file in one go, and set it on the editor using setPlainText.

Related

How to set a thumbnail to my custom file for a preview in Windows Explorer?

In my Qt application, I can save project files of my own type. I would like those files to have a nice preview in Windows explorer, just like picture and video files do by default. Is there a way to do that? I am using Qt, but maybe there is another way.
In other words, if my code for saving a file is as follows, I would like to know what to do in line 5 to make it work:
void saveFile(const QString& fileName, const QImage& thumbnail) {
QFile file(fileName);
file.open(QFile::WriteOnly);
writeInFile(file); // Custom function that saves the project
//file.setPreview(thumbnail); <- What I wish I could simply do
file.close();
}
Setting thumbnail for your own file type is not a Qt thing. It's job of Windows shell, and the behavior is controlled using registery.
Check this if you want to assign a custom icon file.
If you want to generate different preview for each file, just like image file, then check this.
Note they are Windows specific.

How to obtain entire Qt StyleSheet for QMacStyle

Is it possible to obtain a text file of the entire Qt5 StyleSheet for QMacStyle (or its equivalent QProxyStyle that's apparently used in Qt5)?
I'm hoping to get a list of all the property:value pairs ('background-color', 'border-radius', 'margin-top', 'padding', etc.), along with their default settings, that are used for each of the common widgets (QPushButton, QTabBar, etc.).
Qt5 on Mac OSX looks great due to all of the native-looking widgets (see e.g. Macintosh Style Widget Gallery). I'd like to perform some surgical replacements of a number of style properties in my application, but otherwise keep the native look-and-feel. (An alternate take on the problem, or at least on the same end goal, is represented in my related question How to override just one property:value pair in Qt StyleSheet.)
If I knew the entire Qt StyleSheet equivalent for a QTabBar or QPushButton, I could reconstruct the native look-and-feel in my own invocations of widget->setStyleSheet(), adding in my few necessary tweaks.
I've searched the entire Qt5.4.0 directory hoping to find a *.qss file representing OSX style, but to no avail (I found the promisingly named examples/widgets/widgets/stylesheet/qss/default.qss, however all it says inside is //* empty stylesheet *//). I've exhausted my ideas running grep on the full Qt5 source directory, including in qt-everywhere-enterprise-src-5.4.0/qtbase/src/widgets/styles/, which may indeed contain the details but not in very digestible form).
Thanks --
QMacStyle is a QStyle subclass that is using Apple's HITheme for drawing (look for the files qmacstyle_mac* to see the implementation), so there is no stylesheet to obtain.

QVision Widget Error upon compile

Only one error to go until I get to use this for my research!
Warning: Z-order assignment: " is not a valid widget.
FILE: qvvideoreaderblockwidget.ui
There's no line number that came with it. I tried finding but, failed to see an open-ended part.
What should I do to correctly compile this library?
Edit the ui file outside of Qt Creator. Delete the rows with 'zorder' tags. Then open in Qt Creator and compile again. It worked for me then perfectly, the warning did not appear any more.
OK, I had this, and it irked me too. I don't know what happened to cause this, but it is not serious. I suppose that breaking and remaking all the layouts might fix it. Other frameworks tend toward a lot of warnings, but tho I'm new to Qt, not a one yet. I went in another editor, and removed a line that said (as best I can remember):
<z-order>verticalSpacer</z-order>
which was among a lot of other lines which also were z-order tags. I deleted the line with this tag, and rebuilt all. Problem gone. Interestingly enough, all of the z-order tags had vanished from my file when I looked at it next. It must be a bug, but evidently one of little consequence; except those who hate to see the serene beauty of Qt spoiled.
Does the ui-file qvvideoreaderblockwidget.ui contain "Promoted widgets"? If so, use the "Promoted widgets" dialog of the (Qt4-) designer for ensuring that the header of the promoted widget-class is declared as "global include".
In case the custom widget class is named "MyWidget" and the header is named "mywidget.h" and the member widget shall be named "myWidget", the uic will generate the code of the ui-class as follows:
#include <mywidget.h>
class qvvideoreaderblockwidget
{
public:
MyWidget* myWidget;
// ...
};
It happened to me when I deleted a fairly large chunk of stuff in the designer. Investigation showed that the designer had failed to remove a zorder tag relating to the widget I deleted (which, incidentally contained a bunch of other widgets).
Deleting the line as suggested cleared the problem. If you do have a load of zorder tags you probably need to be careful to just delete the one that relates to the deleted item.
I had the same problem, and deleting the .exe file from the output folder (debug or release) before run/compile the source, solved the problem.
This problem arises due to a (presently) unfixed bug in Qt Creator's undo / delete mechanism.
The solution until the bug is fixed is to:
Close the .ui file in Qt Designer
Open the .ui file in an external editor
Delete the <zorder> line(s) that apply to the element with the problem. For example:
<zorder>groupBox_2</zorder>
Save the file
Re-open the file in Qt Designer
Re-compile in Qt

How to display a QImage in QWebKit?

Is there any way to display a QImage (in memory, not on filesystem) in a QWebFrame without writing the image out to a temporary file?
One option might be using the data URI scheme. You basically base64 encode your picture and write the complete data into the URL.
For example:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC
WebKit should support these URIs either directly, or as part of a HTML source code, like
<img src="data:image/png;base64,blaBLABLA" />
Be aware: if your image is big, you might be running into some constraints. Usually this used more for small stuff like icons.
Damn. Steffen beat me to it! :)
To add, you could convert to base 64 by saving the image to a QBuffer(&byteArray) and then saying byteArray.toBase64()

Show standard warning icon in Qt4

I'm trying to display a "warning" icon next to a QLineEdit if it contains invalid data. I was trying to use QStyle::standardIcon(QStyle::SP_MessageBoxWarning) to get a standard pixmap and place it inside a QLabel, and in some cases this seems to work. When running Gnome the icon is displayed correctly, but when running under KDE no icon is shown. I assume that the warning icon is simply not included in the style used under KDE.
What is the preferred way to display a "standard" warning icon in Qt? Does there exist some list which shows which icons are included in every style? How can I get an icon from a style that I know includes the warning icon?
The last time I had a similar problem, I found this Qt labs discussion useful. It informed me that QIcon now (since 4.6 I believe) has a QIcon::fromTheme function that allows you to load an icon based on the Freedesktop.org Icon Naming Specification, and in addition provide a fallback icon to be used if the current theme does not have the icon in question.
What I did was then to include some very basic icons for use as fallback, and in general specify icons only by their Freedesktop names. This gave a theme-consistent look almost always, and the program still worked in cases where people were missing icons.
As for the warning icon, I'm guessing/hoping that every theme must have the one named "dialog-warning", and that it's what you're looking for.
I hope this helps.
Edit: Oh and, in case you don't know, it can be useful to look at for example the Tango icon set to get a rough idea of what the Freedesktop names correspond to (although it is of course theme-dependent).
Qt does bundle a number of images that are resources that you can use in your own code. These images are a superset of those available via standardIcon() You may want to verify that the particular image is included in the versions of Qt you're targeting.
The end result could look like the following:
QPixmap pixmap(":/trolltech/styles/commonstyle/images/up-128.png");
// use pixmap as needed
For anyone who wants to know how to do this in a Windows environment you can:
Create a qLabel in your custom class, and then in the constructor of that class create a QIcon with the style you want, convert it into a pixmap and use the QLabel::setPixmap() function to apply it to the one you created:
QIcon icon = style()->standardIcon(QStyle::SP_MessageBoxWarning); //or
//whatever icon you choose
QPixmap pixmap = icon.pixmap(QSize(60, 60));
ui->iconLabel->setPixmap(pixmap);
ui->iconLabel->setScaledContents(true); //you can set this to fill the
//dimensions of your qLabel if you wish.

Resources