Custom frame for QLineEdit - qt

(Working with Qt 4.8.4)
I'd like to create a custom frame for QLineEdit. One that has been created in photoshop and I have it in three images (as it needs to stretch in width):
left side image
center streetchable image
right side image
I'd like to find a way to replace the look of the QTLineEdit to this, while still retaining the edit visuals look/feel inside of it. It's just the frame I'm looking t replace.
How do I go about it? If I override paintEvent nothing is visible, not even what I type, not even the blinking cursor.

Have you tried:
create your own class CustomLineEdit inherited from QLineEdit,
load/free your pictures in constructor/destructor of CustomLineEdit,
reimplement paintEvent() in CustomLineEdit yourself,
in paintEvent() place all pictures as you need.

Ok. I think I nailed it:
I am setting the following stylesheet for my QLineEdit objects:
this->ui->myLineEdit->setStyleSheet("border-image: url(input_bg.png) 5 5 5 5 stretch stretch;border-width: 5px 5px 5px 5px;");
I am using "5" because in the image I used, a 5x5 cut of each corner encapsulates the area that doesn't stretch gracefully due to gradients within.
this makes sure that the border will work nicely when the edit control is of variable sizes.

Related

Overlay grid onto QFrame

So, I have a QFrame with its layout set as a QGridLayout.
Within this layout I have tiles in rows of 16 which represent something of a palette.
I want this grid of tiles to be separated by lines, like a grid should be. I can do this easily with the tiles' paintEvents.
However, the obvious problem is that between the tiles, the lines are doubled up. When I scale this up for other applications, the difference becomes even more noticeable.
So, is there a way to create a gridline overlay for my QFrame? I have considered converting the whole thing to a view/scene solution, and using drawForeground, however this seems like a completely inappropriate use of the paradigm.
Thanks for any assistance!
Put the QFrame into a QGridLayout, then put a custom QWidget with transparent background and paintEvent that paints the grid on top of it (same QGridLayout position).
Or since you already have a QGridLayout, just put the custom QWidget in that, above the tiles, filling the entire grid.
A side note, are you sure you want QFrame there, or if just QWidget would do? Just saying, because with QFrame you get that 1990's look into your UI... If you do want that then go ahead, just saying.

How to reduce spacing/padding/margin of the text inside QDoubleSpinBox or QSpinBox to zero?

Using Qt 4.8. I would like to set the space between the text of a QSpinBoxes/QDoubleSpinBoxes and the right border of the widget to zero. The left one of the following images is what I got, the right one is what I want:
Note that I use setButtonSymbols(QAbstractSpinBox::NoButtons) to remove the up/down arrows.
The spinboxes are styled using QSS where I can set:
The padding of the QSpinBox/QDoubleSpinBox to 0px.
The padding, the margin and the border of the QLineEdit inside the spinbox to 0px.
I have also created a custom QProxyStyle for the QLineEdit but I'm not able to find any related pixel metric enum that I can use to reduce the space
I can also try to mess around with the internal QLineControl class which is not possible without fiddling inside Qt's code base. (I have not tried this yet!)

How to give QTextFrame or QTextBlock a background-image in QTextEdit?

I am developing an IM tool,as a part of it I have to develop a BubbleChatWidget on which all message items have a bubble-like background-image.I thought I could achieve my goal with QTextEidt,but I don't know how to give 'QTextFrame' or QTextBlock a background-image.
So my question is that how to give QTextFrame or QTextBlock a background-image in QTextEdit?If QTextEdit can't satisfy my demands, how to achieve my goal with other Qt techniques?
The BubbleChatWidget may contains clickable texts or pictures.And you can't forget the fact that the BubbleChatWidget may contains thousands of items.
The picture below displays what I want.
Qt Style Sheets are a perfect fit to achieve what you want.
The solution is to use a border image. Fortunately you can do that with style sheets and you can style QTextEdits.
About styling the QTextBlocks or QTextFrames: A QTextEdit is a widget that displays a QTextDocument which can contain QTextBlocks and QTextFrames. Frames and blocks are text containers that provide structure for the text in a document but they're not rendered by separate widgets. For that reason they can not be styled independently. What I recommend is to use a QTextEdit or other widget for each message and properly manage the consequent increase in memory use.
I'll expose how to style a text edit.
First, take a clean image of your desired border. With a little Photoshop I've prepared my own image (not as clean as it should for a production app):
Lets style objects of the class QTextEdit and its subclasses.
QTextEdit {
background-color: #eaedf2; /* Same gray in your background center */
border-image: url(":/images/bkg.png"); /* The border image */
border-top-width: 11px;
border-right-width: 4px;
border-bottom-width: 4px;
border-left-width: 11px;
}
Setting the previous style sheet to the container of the text edits will turn all of them into this
Something quite similar to your desired look. Of course you have to prepare a good border image and better adjust the border dimensions but I think this could be of help.
If you want different styles for incoming and outgoing messages then you will have to
properly differentiate and select them in the style sheet. Check this for reference.
You need to modify Qt's source code for this.
I've done this last year.
You could set a background image (the bubble image ) for a frame.
If you r using Qt 4.8.6, locate qtextdocumentlayout.cpp, at line 402:
The default fill fillBackground implementation is
p->fillRect(rect, brush);
You could change to qDrawBorderPixmap instead to draw a bubble background.
You can use QBalloonTip which is an internal class defined in
QtDir/5.*/Src/qtbase/src/widgets/util/qsystemtrayicon_p.h
QBalloonTip inherits QWidget and it is implemented in qsystemtrayicon.cpp at the same directory. It has the following method to show a balloon tip:
void QBalloonTip::balloon(const QPoint& pos, int msecs, bool showArrow)
You can modify the source code of this class to have your desired balloon tip.
If you are interested in open source project that implements the same thing(I call it 'Bubble Chat'), you can search Telegram Desktop and Cutegram in Github.
They are both clients of Telegram or its variant implemented by Qt/Qml.
I think Cutegram is better.
Hope this tip will help you.

QPushbutton background image from 3 images

I have three background images for a button - left.png, center.png and right.png. The left and right ones have rounded edges and the center one is a single line which need to extend based on the size of the button. How do I create such a button? I have considered the option of constructing the image on the fly and apply it to the button in the resize event, but am looking to see if this is possible through stylesheets. Is this possible?
You can't do it with background-image alone. But it may be done with the help of border-image: http://qt-project.org/doc/qt-4.8/stylesheet-reference.html#border-image
Use background-repeat: repeat-x to fill the button and border-image to round out the edges.
You can do it with QPainter and constructing the image, but honestly for the complexity level and for a button class you might be better off learning how to do it by constructing a paintEvent in full using the standard primitives. It'll give you better fine grain control, just involves a bit of leg work.

Qt QHboxLayout cell size ssues

For those of you who haven't been reading my Qt questoins, I am learning Qt for a project. I have only limited experience with GUI design at all, and not in Qt.
I've got a horizontal layout that I want to populate with some buttons. I can feed these buttons in just fine, but my formerly-square buttons are stretched horizontally to take up more space.
I want to let the layout manager determine the best way to size these buttons, but I also want their original proportions to remain intact. For instance, if I start would with 32X32 buttons that need to shrink to fit all of them in the layout, I want them to shrink proportionally so that the width to height scale is maintained. 20X20, 16X16, 12X12 would all be just fine, but 24X16 would be an example of dimensions that are unacceptable.
I've tinkered with size policies on the buttons and stretch options. I'm not seeing, even after reading the QPushButton and QHboxLayout classes how to do this. How is it accomplished?
Thanks.
As long as I understand the question correctly, I think what you want is QBoxLayout::addStretch(). This will add a spacer object that fills the unused space. So the buttons will have their ideal size and the spacer will fill the rest. You can try experimenting with this in Designer, it's easier than the write/compile/run cycle.
You should take a look at the answers to this question. This is a recap of my answer there.
You need to create a custom derivative of QLayoutItem, which overrides bool hasHeightForWidth() and int heightForWidth( int width ) to preserve the aspect ratio. You could either pass the button in and query it, or you could just set the ratio directly. You'll also need to make sure the widget() function returns a pointer to the proper button.
Once that is done, you can add a layout item to a layout in the same manner you would a widget. So when your button gets added, change it to use your custom layout item class.
I haven't actually tested any of this, so it is a theoretical solution at this point. I don't know of any way to do this solution through designer, if that was desired.

Resources