Add some text to a QToolBar - c++

I am having trouble adding some text in a QToolBar. I can only add Actions. Also I have two actions with their rerspectives icons in my QToolBar but I want to separate them and I canĀ“t either.
My newbie approach was to add empty actions to simulate blank spaces between the icons. But the user can click on the blank spaces.
I am using the Design function of QT Creator. Some help would be really apreciated.

It looks like you can't do it from within Designer:
https://bugreports.qt.io/browse/QTBUG-1267
That's an oooold suggestion, too, with a low priority to boot, so it probably won't get fixed any time soon.
You can get your hands dirty and do it in code, however.

Related

Set QTabWidget Order by text of tabs

I am trying to set tab order by tab text or tab name
ui.tabWidget->setTabOrder(//set order here by tab name like "helloTab","hiTab");
//I have tried this(I know it's dumb just trying)
ui.tabWidget->setTabOrder(ui.tabWidget->tabBar->findChild<QTab *>("tab_1"), ui.tabWidget->tabBar->findChild<QTab *>("tab_2"));
Till now no hope of getting this done. I am fairly new to qt.
Any Ideas or help would be helpful.
QTab is not defined in Qt. Use QWidget:
ui.tabWidget->setTabOrder(ui.tabWidget->tabBar->findChild<QWidget *>("tab_1"), ui.tabWidget->tabBar->findChild<QWidget *>("tab_2"));

Hide Page/Tab from QTWidget - QT 5.5

i need to hide tabs from a existing project in QT, i don't want to delete the code because i have to set parameters on that code, the Application relay on that too. Seems like QT hasn't built-in hide(); function, i tried to edit stylesheet to make it smaller, but doesn't work too, i've looked on the internet and seems like this is a known issue. Does somebody have some tricks to avoid this?
Only thing i was able to come up with is:
ui->TabObject->setEnabled(false);
basically i disable objects in the tab to make them not usable by the user, but this is not a good thing for the whole UI.
Maybe by calling QTabWidget::removeTab(index) - this removes the tab from the QTabWidget, but does not delete the tab's QWidget.

Qt - How to create extra roles for a QDialogButtonBox

I apologize in advance for not having more code to show, this is more of a conceptual question. I am working in Qt 4.7 and I have a QDialogButtonBox in my UI to which I need to add several custom buttons*. I set the buttons up like I normally would. For example, if I'm adding a Save button, I would create it like this:
QPushButton *myButton = new QPushButton(tr("Save"));
Usually I could just add this to my UI as is. However, I've found with the button box, it needs to have a "ButtonRole" attached, which are built-in to Qt. The only role that seems close to matching what I want is "ActionRole", but that's still sort of vague as to it's meaning when looking at it later in the code. I know theoretically any of the roles could be associated with this button, but it seems like really bad practice to me to attach an unrelated-named role like "RejectRole" or "HelpRole" to it just to make it work. My question is, how can I create a new role, something like "SaveRole", that I can use for this button? I tried putting a line like #define SaveRole (some int value) in my code and using that since the ButtonRoles are enumerators, but that gave me an error saying it couldn't convert the parameters. I know there's also NRoles in ButtonRole, and it seems like that can probably be used to create new Roles, but I've been searching for about a half hour and am finding it EXTREMELY difficult to find any information on how to use this. If anyone has suggestions it would be greatly appreciated. Thanks!
*For the record, I know how easy it would be to avoid this problem by using QPushButtons individually instead of a QDialogButtonBox, but my project head wants the button box used, so unfortunately I don't have that option.
EDIT: I forgot to mention before but it may be worth bringing up, this button box is pretty large, and all the built-in roles are already in use.

Making a game in Qt regarding GUI windows

I've been wanting to program a simple game with a simple GUI using Qt (Its will be a VERY simple game, nothing fancy). What I've been wondering is, how can I create multiple windows and display them when needed? For an example, a battle screen and an inventory screen. The user should only see one of them, but should be able to access the other one when needed. I was using stacked widget but I'm not sure if that's the proper way. Also, is it better to design the windows in the designer or to code them?
A StackWidget certainly would accomplish what you want to do. The reason why it is not always used for this kind of thing, is that it all the screens are pre-created at the beginning and always exist. This means it takes a little longer to initialize, and you are using more resources than you need at any one time
But as you are saying, if this is a simple game, then I don't see a big problem with it. Just as easily, you could also, create an empty layout and swap the inventory and game panels as needed.
Add my vote to everyone else suggesting to use the designer. It is so much easier to manipulate layouts, actions, and such using the designer then through code.
You can see the Designer manual here
So this is what I would suggest:
Create your "battleScreen.ui" - which is the designer file for your battle screen and everything in it, and then create your "inventory.ui". Both of these could be QWidgets, or QFrames, or whatever makes sense.
Then create your "Game.ui" which will be your QMainWindow.
In your Game main window, you can then add your QStackWidget, and place your inventory, and battle screens in the stack widget.
If you don't know how to do that...
1) drag a QWidget into your form (into the stack widget)
2) select the new QWidget and right-click.
3) Select "Promote to..."
4) Fill out the information to promote the QWidget to your inventory class
Promoted Class Name: The name of your inventory class
Header File: The header file of your inventory class
5) Click add
6) Click Promote.
Hope that helps.
Since I'm not sure what your goals are I can't advise whether or not the stacked widget is appropriate but I think you can accomplish quite a lot using the designer and style sheets. If you need to code some parts of the GUI, you can always drop in a place holder widget and either replace it with coded items or make them children of the place holders.
A general answer for a general question:
Use the Designer to create your windows; hide and show the auxiliary windows as needed.
Use a flow manager class to manage the visibility of a related set of windows.
The stacked widget is useful for managing a button/icon whose appearance changes based on state; the different representations live in the stack.

Qt: How to show icon when item selected

I have a QListWidget containing items which have icons and when the items are selected the icon is just highlighted out. Is there a way to prevent this? I can't use stylesheets because it's for an embedded application and including them takes up too much space.
thanks
I suppose when you say "Highlithed out", you mean that the icon colors don't render well when the line is selected, and therefore, you can't see properly the icon...
Maybe you could consider using a different icon when the item is selected. It's possible to do so by specifing a mode to your icon.
Example :
QIcon MyIcon(":/images/foo");
MyIcon.addFile(":/images/bar", QSize(...), QIcon::Selected);
You can easily make a try in QtDesigner and see the results...
Hope it helps a bit !
Certainly, drawing on a black-and-white screen presents its challenges.
It sounds like you just want to change the appearance of the interface, not any functionality. If this is the case, a QItemDelegate-derived class (or QStyledItemDelegate) is almost certainly what you want. In particular, the drawDecoration function looks like it is used to draw an icon, and the style options should include whether it is selected. The simplest fix would be to override that function, set the selected flag in the options to false, then pass it up to the parent's function.