How to create legend in progress bar? - qt

how to make number follow the bar size?
in this qt designer, i create frame which contain bar, and
qlabel =500\n\n250\n\n0\n\n-250\n\n-500
Is there a way to create legend in this bar?
or can we make qlabel follow the size of bar?'
Progressbar in normal size
Progressbar that get extended

If I wasn't interested in having accuracy, I would create a structure with layouts like this
QHorizontalLayout
-> QProgressBar
-> QVerticalLayout
-> QLabel (500)
-> QLabel (250)
-> QLabel (0)
-> QLabel (-250)
-> QLabel (-500)
But If accuracy is your priority you should start thinking of making your own custom widget.
p.s. the above is not code, but rather a representation of the widget hierarchy that I suggest. I have just formatted it as a code snippet, just to show the hierarchy :)

Related

Qt - How to add edge nodes onto a QWidget which enable users to drag to resize QWidget

I'm doing a screenshot feature. And once the select area (QWidget) is decided, user can drag one of the edge nodes of the select area so that it can be expanded to capture more contents of the screen.
The picture below shows what I expect to do:
But I have no idea how to create these nodes on the edges of a QWidget - the Select Area. Or maybe there is something subclassed from QWidget class which can help?

How to prevent QTableWidget from occupying the whole window in a QHBoxLayout?

In my Qt program, I programmatically generate a modal QDialog. I want to show two widgets in this dialog window: A custom widget showing a camera output and a QTableWidget, showing the pixel coordinates of the corners found in the camera image. I generate a QHBoxLayout and add my custom widget and the QTableWidget into it. Then I set this QHBoxLayout as the Layout of the QDialog window. What I want to achieve is to share the available space in the QDialog's window area equally between my custom QWidget and the QTableWidget, horizontally, by using a QHBoxLayout. But I always end up with QTableWidget occupying the whole QDialog area, by overlapping my custom widget. How can I instruct these two widgets to exactly share the QDialog area?? Note that I first add my custom widget and then the QTableWidget into the QHBoxLayout.
Make sure on your custom widget you've specified a minimumSizeHint and a sizeHint, this instructs the QLayout manager that the widget requires a specific space. To have them split equally you'll be best off detecting the size of the QDialog and then specifying the width for both by removing the boundary sizes (spacing between widgets + space to QDialog edge) and dividing it up.

Qt layout children float

I have an empty horizontal layout that I dynamically populate with three children. This is a card game so this layout gets dynamically populated with children that are QLabel which have an image set. When I click on a card to play it and move it at the center of the screen I'd like the positions of the other cards to stay the same. Instead the cards tend to "float" to the right. How can I force them to stay in their original position?
+-----------------+
| |
| [A] - [B] - [C] |
| |
+-----------------+
I tried putting an expandable horizontal spacer in between each card but it doesn't really work. I also tried with replacing the horizontal layout with a grid layout but this forces me to specify the initial coordinates of the cards which I'd rather not. I like the fact that I can append widgets with addWidget pretty much like I do in the HTML version.
In the HTML version I use some wrapper divs with a fixed size around the cards. How can I do something similar in Qt?
EDIT
A little clarification on how the layout should look when one card (B) is played.
+-----------------+
| |
| [A] - - [C] |
| |
+-----------------+
As I mentioned I managed to fill the layout with empty QWidget and put cards on top of them (not inside them). I actually got also rid of the horizontal spacers I had as they are not needed anymore. This seems to work as I expect, although I still didn't fully validate this approach.
Simple algorithm steps to do what you want :
User clicks
Select the label where the click happened
If there is an image, Remove the image from the label
Don't remove the label from the layout, and make sure that the layout don't resize the label
Add the image to a label in the center of the board game.
When the user clicks on a label which has nothing, just ignore the click.
For each player you know the maximum numbers of cards which can be shown in your layout. Moreover, I don't think the size of the card is variable. So each time you create a deck for one player, create empty labels, each one having a fixed size.
EDIT: SOME OTHER PROPOSITIONS
fix size of labels using QWidget::setFixedSize();
Don't hide your label to remove your pixmap . Don't set an empty pixmap. Rather, set
a pixmap which is transparent or filled with some neutral color...
Alternatively subclass QLabel such that you can indicate a minimum size hint. The layout must and will respect anything you set. Example:
class MyQLabel : public QLabel {
MyQLabel(QSize cardSize, //remaining qlabel args)
:QLabel(//label args)
{_cardsize = cardcardSize;}
//only method to re-implement
virtual QSize minimumSizeHint () const {return _cardsize;}
private:
final QSize _cardsize;
}
Each time your whole application need to be painted again, all widgets properties are checked. My two cents is that a size property is modified when you play a card,
and the layout adapt to this modification. Tell me if the size hint trick work (it should).
In Qt you can use wrapper QWidget with Fixed sizePolicy.

Qt : event invisible widget?

For some reasons, I need to draw a widget onto one another.
The structure is the following (see the image) :
I have an original QTableWigetItem
On the QTableWigetItem, I create a QWidget at the foreground with the same geometry
This QWidget contains a QBoxLayout
This QBoxLayout contains a QPixmap and a QComboBox
I want to do the following things :
The QWidget is just a "container" for my QBoxLayout and I would like to set him completely "invisible" for the user. If the user click or move at the position of the widget, I want the event of the QTableWigetItem in the background to be trigerred. But the problem is that I want the QPixmap and the QComboBox to be at the foreground, visible and "normal". For me it's just a trick to be able to put children widgets in a QTableWidget of a HeaderView.
How to make the QWidget "completely invisible" (from the event/signals point of view) ?
Thank you very much.
Try QWidget::setWindowOpacity(0)

How to place one widget over another in Qt

i have a window in Qt, on that i am drawing a picture. now i want to place the progressbar over it.
how can i do that?..
steps i am following to do
Create a window,
Draw picture in paint event of window
Then create QGridLayout layout, add your window
Display over it.
suppose i want to add progress bar, over a portion of picture window. how can i do that
i dont think its possible to implement in window paint event.
please assist me
Thanks
You can add the progress bar as child of your QWidget without adding it in the layout. This will draw the QProgressBar into the QWidget. Since you are not using the layout you will have to manually manage the position of the QProgressBar.
I think that just adding a progress bar widget to your grid layout should work.

Resources