I'm trying to do an application with the Qt framework. My main problem with this framework is the design. I made a lot of research and I find two ways to achieve the style of my app.
Qt StyleSheet
Qt Style class
I find a lot of people talking about the QtStyleSheet but almost nothing on QStyle.
What's the best solution ? Have you examples with QStyle ?
I'm trying to programming a QStyle class but I find it too complex. The Qt documentation it's not enough for me to understand all the topic.
Here is a snippet of my code to do a Transparent PushButton with a special Font and a margin. (code in a drawControl function of my QProxyStyle class)
// draw background
QRect button_rect = button_option->rect;
// draw background
button_rect.adjust(12, 12, -12, -12);
painter->fillRect(button_rect, QColorConstants::Transparent);
painter->setPen(QColorConstants::Black);
// define the font
QFont font("Material Icons");
font.setPixelSize(24);
painter->setFont(font);
// draw text
painter->drawText(button_rect, Qt::AlignCenter, button_option->text);
QStyleSheet is like a CSS (but here we call it QSS). With QStyle and overriding events, you have more flexibility. , if you'd like to have so much flexibility, you can use QStyle + overriding events and more helper classes like QPainter, etc.
but if you'd like to have some magic changes with a few lines, I suggest that you use QSS.
I do not exactly know what is your question. But yes, you are right that overriding QStyle is very complex and is not well documented. If you want to make your own QStyle, then painting buttons is probably the most simple thing you will encounter. Drawing more complex widgets such as combo boxes scrollable things, tabs, etc. might be a nightmare. If you want to have a look at an example of quite nicely overriden QStyle, have a look here https://github.com/randrew/phantomstyle
Using stylesheets is much simpler but it also has several drawbacks and rendering is much slower than without stylesheets.
So I cannot provide any concrete advice. The decision will be up to you and up to your requirements.
Related
I'm looking to make a sprite animation editor. I have the loading of my custom animation file done but now need to get the actual ui started. I'm really just stuck on what widgets I would use to actually play my animation. I need to be able to go to certain frame, play, pause, loop, etc. Once I'm done with the viewing portion I plan on adding in the editing.
I've seen AnimatedSprite in qt docs but that seems to only allow playback of sprites in the same file. In my situation sprites can be from multiple image files and sometimes doesn't follow a grid like sprite cutter.
First of all, you should decide whether you want to use QML or Widgets. AnimatedSprite is QML related class. All widget-related classes starts with "Q" letter.
If you decide to use Qt Widgets, I would recommend to take a look at Qt Animation Framework in combination with Qt Graphics View Framework. Most likely it will not let you do everything you want out of box, but it should provide you with a rich set of useful tools.
If you need here are some examples.
Hope it helps.
Have a look at QMovie. This class may provide all the methods you need, as long as you only want to use it for viewing. The QMovie can be passed to a QLabel to show the animation.
QMovie however supports only gif out of the box (and there is a third party plugin for apng files). You would probably have to create your own image handle plugin to support your format.
If thats not applicable or to complicated, you will most likely have to create your own custom widget. Have a look at the painter example. Playing an animation is not that hard if you have all the frames. A simple QTimer to change the image to be drawn in a constant rate should work.
in my application there is the QProgressBar I use, but it's not quite illustrating progress of a process, rather it represents a state. Therefore, I want it not to animate on Mac, as it always does. I have no experience with Cocoa, but Qt does not allow me to do that. And therefore I ask some more experienced Cocoa programmers: is it even possible to stop the progress bar animation? I looked for that for quite a while, but I didn't find anything.
Thank you in advance.
You could either call setStyle() on the QProgressBar to use a different kind of QStyle instead (i.e. one that doesn't animate); or you could ditch the QProgressBar altogether and use a different widget type entirely (e.g. a QLabel, or just subclass QWidget and implement paintEvent() do draw whatever you want the widget to look like)
I'm making a game in XNA, but since I have people with different operating systems, I'd like to make the mapeditor in Qt. I have C++ knowledge, I'm new to Qt though.
I'm looking for something to draw images, or rather parts of them, in an area of my application.
Something like spriteBatch.Draw, maybe. Important would be that I can define a source rectangle of the picture and a destination rectangle.
Since I'm new to Qt, detailed explanations or links to tutorials would be really appreciated.
Regards
Depending on how you want to do this, check the Qt Paint System documentation, or the Qt GraphicsView Framework.
The Qt Examples pages have lots of well documented samples.
Also check out the documentation of the QPainter and QImage classes.
I want to show a car's path in a window in real time, how to do that, and what classes I have to use. Is there anything like drawing area, in Qt.
Any Help will be appreciated.
You'll need to read up on QGraphicsview. There are several helpful examples that show every bit of what functionality is present here. The implementation itself... I guess it's just a bunch of QLines on a QGraphicsScene. The realtime part is handled by calling repaint or paintevent or whatever it's called periodically, or setting up a complex animation.
The Qwt library on top of Qt is pretty good for this.
I have a custom Qt widget which I used to display disassembly and I am looking to add syntax coloring to it.
Currently, I simply set the QPen to a solid color, construct the text I want to display, and render it to the QPainter at the appropriate coordinates.
The question is, what is the best approach to adding syntax coloring? I've thought of a few:
I could simply divide the coloring into logical blocks, each preceded by setting the QPen to the desired color.
I could have special escape characters which represent a change in the color palette, and render 1 character at a time.
I could do a modification of #1 and create a list of std::pair<QColor, QString>, then I could simply iterate the list setting the color and drawing the text as I pop items off the front of the list.
Something entirely different?
I know that each of the 3 approaches I've listed will technically work, but I'm looking for a very efficient solution. This code will be called a lot. And since this is an interactive debugger, if this code is slow, someone rapidly stepping or tracing will see a visible slowdown.
EDIT: I'm aware of QSyntaxHighlighter and QTextDocument. The main issue is that these don't generally suite my purposes very well. I have several columns which all have dividers and can be slid back and forth. To give you an idea, Here's a link to a screenshot of my debugger. As you can see it isn't really like a text document at all. In fact it is closer to a list or table. But there is already a bunch of custom drawing going on making a normal QTextDocument somewhat impractical.
EDIT: I was incorrect, It seems that QTextDocument can render directly to a QPainter. Looks like what I need!
EDIT: It is unclear how to control where and how QTextDocument or QTextLayout will draw on a QPainter. I've attempted to use them to no avail. So if someone could provide a rudimentary example, that would be very helpful.
EDIT: I was eventually able to get what I wanted using something like this:
painter.setPen(default_color);
QTextDocument doc;
doc.setDefaultFont(font());
doc.setDocumentMargin(0);
doc.setPlainText(text);
highlighter_->setDocument(&doc);
painter.save();
painter.translate(x, y);
QAbstractTextDocumentLayout::PaintContext context;
context.palette.setColor(QPalette::Text, painter.pen().color());
doc.draw(&painter, context);
painter.restore();
Qt provides a QSyntaxHighlighter that is probably exactly what you want. QSyntaxHighlighter uses a QTextDocument to mark each block of code with a specific state which can be associated with a specific presentation format.
The documentation on QSyntaxHighlighter provides a sample demonstrating how this may be accomplished and does some nice things:
Separates the model from presentation
Separates the formatting into different reusable classes (if implemented as such)
Supports the State design pattern if useful to your language
I'd use either QTextEdit or directlay its underlining engine QTextDocument.