How to put a QLabel in a toggle button Qt c++ - c++

For my project, I wish to have a toggle button.
After much research I found this post. toggle-switch-in-qt
I implemented the code of the third post on the link above.
This code works correctly. By cons I do not understand how can we add QLabel for the two states of the toggle button?
Would anyone have an idea to do this operation?

You will need to draw the label manually in Switch::paintEvent, depending on isEnabled(), using QPainter::drawText. Since the paint event of the control you refer to is overridden, the default behavior in QAbstractButton::paintEvent which normally draws the button label is not executed.

Related

Is it possible to disable the light-blue mouse over highlighting on a QTreeWidgetItem?

I've a QTreeWidget and need to disable the mouse over highlighting on the childItems but not the click selection. The point here is, that I need to set this per Item because some are selectable. I was thinking about the QTreeWidget::itemEntered signal to check if the item should be highlighted or not but I can't get it to work because the description says
QTreeWidget mouse tracking needs to be enabled for this feature to
work.
and I can't figure out how.
So my questions for are: How can I enable mouse tracking?
Is there an easier way to disable the highlighting?
Simply invoke setMouseTracking() to enable mouse tracking for a specific widget.
I ran into this problem (I know this is an old post, but I might as well post my solution, since it can be useful for others).
I could not properly disable the mouse feedback while keeping the mouse tracking enabled, but I could make this feedback invisible. I'm using qss stylesheets, and I set the mousehover feedback color to transparent:
MyTreeWidget::item:hover {
background-color: transparent
}
It did the trick for me. Sadly it makes the feedback invisible all the time, rather than allowing to turn it off and on.
So as a next step, for when I needed it, I implemented my own feedback by using a delegate and overwritting the paint function.
The QTreeView overwrite mouseMoveEvent and sends mouse coordinates to the delegate. This way, the delegate can adapt what it does in paint to this position. It feels pretty heavy, and a bit dirty, but it works. Delegate should also allow to have different behavior for different items.
PS: If you're using a delegate, in most cases, that should be enough without the qss change. In my case it wasn't, because I call QStyledItemDelegate::paint in my overwritten paint method, so I inherited some unwanted behavior.

How to activate centralWidget in QT Designer

I was looking at this article
How to make a Qt Widget grow with the window size?
but when i got to the answer I got stuck on "activating" the central widget. I notice an icon with a red circle so I guess that means its disabled. I've been searching the net to try to figure out how to "activate" it but I am not having any luck.
Can someone please help me out?
Have a look at the layout system.
That icon does not mean your QWidget is disabled, that just mean you do not apply a layout on it.
Try to press like Ctrl+1 in order to apply a basic layout. If nothing has changed, you might need to put a QWidget inside the central widget first and then apply the layout.

How to add a widget to a qt Tool bar

I have the following problem:
I have added a spinner to my qt Tool Bar, which is located from top to buttom. This works fine. But now I want to order some buttons in a special order, but with mainToolBar->addWidget(button_name) it would be among one other. So how can I solve it? I tried to make a new widget "widget_1" and added some buttons to this widget, but when I write mainToolBar->addWidget(widget_1) nothing appears, only the one slider I have already added. Can anyone help me?
Thanks a lot :)
Well, just forget something.
select the widget and then layout in a form or whatever layouting you prefer.
And that's it. now, the widget will be shown in toolbar. it's because of size
and child widgets position. by layouting, everything will be resized and position
correctly.

gtkmm button coloring

I'm trying to change the background color of a button upon clicking it. I've connected the button to a clicking method just fine, but I can't seem to find the correct c++ syntax to create this. I've seen it done in python, but that doesn't exactly help me. Anyone have a tutorial or know the syntax?
EDIT: That makes sense. Thanks!
Buttons don't have color, they contain a child object and emit a signal when pressed, that's it.
You are likely putting a Label in the Button as the child object. A Label is text rendered by Pango, which lets you set attributes. What you think is the Button color is actually the background color of the Label text.
Gtk is pretty complex, but lets you do anything. If you want to do much with Gtk, look for a tutorial on Pango (text) and Cairo (images). If you want a simpler self-contained widget set, check out wx or tk.

mouse over transparency in Qt

I am trying to create an app in Qt/C++ with Qt4.5 and want the any active windows to change opacity on a mouseover event...
As i understand it, there is no explicit mouseover event in Qt.
However, I got rudimentary functioning by reimplementing QWidget's mousemoveevent() in the class that declares my mainwindow. But the mainwindow's mousemoveevent is not called whenever the mouse travels over any of the group boxes i have created in there (understandbly since QGroupbox has its own reimplementation of mousemoveevent).
So as a cheap work around, I am still using the mousemoveevent of my mainwindow but a query the global mouse position and based on the (x,y) position of the mainwindow (obtained through ->pos()) and the window size (-> size -> rHeight and rWidth), I check if the mouse is within the bounds of the area of the mainwindow and change the opacity thus.
This has had very limited success. The right border works fine, the the left changes opacity 4 pixels early. The top does not work (presumably because the mouse goes through the menubar and title bar) and the bottom changes way too early.
I thought of creating an empty container QWidget class and then place all the rest in there, but i felt that it would still not solve the big issue of the base widget not receiving the mousemoveevent if it has already been implemented in a child widget.
Please suggest any corrections/errors I have made in my method or any alternate methods to achieve this.
p.s. I doubt this matters, but I am working Qt Creator IDE, not Qt integration into VS2008 (it's the same classes anyways - different compiler though, mingw)
Installing event filters for each of your child widgets might do the trick. This will allow your main window to receive child events such as the ones from you group boxes. You can find example code here.
You may be interested in Event filters. QObject proves a way to intercept all events zipping around your application.
http://doc.trolltech.com/4.5/eventsandfilters.html#event-filters
If I understand what you are attempting to do, I would reimplement the widget's enterEvent() and leaveEvent(). The mouse enter event would trigger the fade-in and the leaveEvent would trigger the fade-out.
EDIT: After re-reading several times, I'm still not sure what you are trying to accomplish. Sorry if my suggestion doesn't help. :-)