How to add QMenus or Qactions on a Widget like QlistWidget area as a list item? - c++

Is there any way to add QActions as a list item on QListWidget?
I want to make a customization window which will show list of actions on a widget for move up, move down, Rename and other options. I'd like to display it on the widget same as it appears as a context menu.
I tried adding it as a list item with icon and text, but the look it not very good:
i) list items with blank icon are not aligning properly, even after adding a blank icon of size 16*16 is not taking up any space and text with icons & w/o icon are not aligning.
ii) I'm unable to add right-pointing black triangle at the right most, in-case of sub-menus cause somehow unicode character for this is not getting displayed on my Linux machine.
That's why I want to add QActions as it are getting popped at original place.
Any suggestions?

Yes I have a suggestion : do not try to make fancy widgets like this, users will not find it intuitive
You should find another way to implement this.
Imo, something like a QToolButton with a QToolButton::MenuButtonPopup popup mode will do the trick. This way, you can embed menu and sub-menus in a widget, using QToolButton::setMenu().

Related

Creating a scrollable window without Layouts

I am trying to achieve something what, I thought, would be a super easy thing to do. But for some reason QtDesigner is driving me crazy, it simply won't work...
I created a GUI and freely arranged different elements in the window, without layout or anything like that. At some point there were to many elements, so all I wanted to to, was to make it scrollable up and down, to see all elements.
So I added a ScrollArea in QtDesigner and added all elements as children of this ScrollArea (which btw also was a pain in the ass, because apparently drag and drop in the Object viewer is not a thing, and editing the .ui file by hand, is also not allowed... great).
So the result I have now is the following:
before resize - no scrollbar, elements at bottom inaccessible
resized vertically - some stuff still snapped off at the bottom
So as you see, although I created a ScrollArea... There is no scroll area. So I googled a little bit and found out that you can add layouts to your scrollarea, and yey, finally, a scroll bar! But how in this world am I supposed to arrange the elements in the way you see in the screenshots, with layouts. They are so super restrictive.
How am I supposed to simply get a vertical scrollbar, without this restrictive layout stuff?!
Here is how my object viewer looks
And here is what is called upon GUI creation:
ui->setupUi(this);
//setCentralWidget(ui->scrollArea);
//ui->scrollArea->setWidgetResizable(true);
I tried it with, and without the commented lines. No scrollbar, no matter what I do.
Try this to fix it:
In Qt Designer:
Select QScrollArea object.
Uncheck the QScrollArea properties widgetResizable.
In C++:
// If you want to set `widgetResizable` programmaticly
ui->scrollArea->setWidgetResizable(false); // Optional if you did it in Qt Designer
ui->scrollArea->widget()->adjustSize();

Expandable list view with customized scrollbar in Qt

I want to build a widget like this one that we can find in Word :
So, there is a list view using a specific scrollbar with 3 buttons and no scroll.
When you click on the last button at the bottom right, a new list view with a classic scrollbar is shown over the previous list view (hidden when losing focus). So basically, the smae behavior as the one in Word.
We are already capable of displaying a list view with custom content.
My main concern is how to build the widget in the first image: the list view with the custom scrollbar (3 buttons, no scroll)?
What is the proper way to do this ?
I assume that you're implementing a subclass of QAbstractListView.
I don't believe you need a custom scrollbar - just put the scrollbar and the button into a QVBoxLayout; hide the button once it's checked (you could even connect its toggled() to its setHidden() for that).
At first hide the default scroll-bar by calling the QAbstractScrollArea::setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) method.
Then create your three buttons on the left side and connect the QPushButton::clicked() signals to some slots where you just scroll your list by calling the QAbstractItemView::scrollTo(index), QAbstractItemView::scrollToBottom() or QAbstractItemView::scrollToTop().
While it is correct that you could just build a custom widget consisting of a view with hidden scrollbars and add the buttons to the layout, connecting the signals/slots needed to provide the proper actions, you can also consider to implement your own QScrollBar class. QListView is derived from QAbstractScrollArea, which provides setVerticalScrollBar() so you can just set an object of it to be used by the view. The integration of scroll area and scroll bar should be much more straightforward this way, but you'll have to do the painting of the scroll bar's content yourself, or put a layout and the buttons in there (QScrollBar derives from QWidget, and you'll have to reimplement paintEvent()).

How to add widgets positioned relative to a tab in a QTabBar?

Is it possible to add some widgets to a QTabBar? I wanted to have a QComboBox to the side of the last tab, and have it only appear when the last tab is selected.
It's possible to add child overlay widgets to any widget, so the answer is: sure!
You can hook to the tab widgets's or tab bar's signals to get notified when the last tab is selected. Then use tabRect() to get the rectangle of the last tab. Position your combo box to the right of it. It'd need to be a child of the tab bar. That's it.
It might be easier to use a QStackedWidget to get your desired results. When you are using the QStackedWidget you can have different buttons outside that reveal the different widgets. Then use some custom signal for when the last button is activated to show a combobox that appears next to the last button.
Here is the link to the QStackedWidget

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.

MFC child dialog changing size unexpectedly

My application uses stacked dialogs to select between options in several places. For example, the dialog box below uses two stacked dialogs:
To choose between "shooting methods", the user selects from the drop-down list in the bottom right. This changes a child dialog box above it.
The "advanced options" box (located in the child dialog box) selects between a simplified interface and a more complete one.
In each case, the stacked dialog box is implemented using a picture object as a placeholder in the parent dialog. When a page is selected, SetWindowPos is called to move/resize the child dialog (pNewPage) to fit the placeholder.
// Show the newly selected page
pNewPage->ShowWindow (SW_SHOW) ;
pNewPage->SetFocus () ;
// Position the newly selected page
CRect rcDlgArea ;
GetDlgItem (IDC_DLG_AREA)->GetWindowRect (&rcDlgArea) ;
ScreenToClient (&rcDlgArea) ;
pNewPage->SetWindowPos (this,
rcDlgArea.left, rcDlgArea.top, rcDlgArea.Width (), rcDlgArea.Height (),
SWP_NOACTIVATE) ;
This has worked very well up until now, but one of my users in Germany is having a problem I can't explain. When he opens the tool, the stacked page comes up looking like this:
Note that the child dialogs are stretched so that the text in the child dialog appears larger than the text in the parent.
Other than the visual layout issues, the child dialog also seems to "cover" the selection drop-down in the bottom right (located in the parent dialog). Although the drop-down is still visible, CBN_SELCHANGE messages are not received when the drop-down list is clicked.
I am at a loss to explain why the child dialog boxes are being rescaled. As you can see above, I've tried to be very explicit about the resizing of the dialog box, but this doesn't seem to work.
Can anyone think of a reason why the child dialog might be rescaled on some systems but not on others? Any help would be greatly appreciated.
Thank you,
Michael
Seems like this user has larger fonts selected than what is used in the first screenshot. Note that dialog sizes are specified in DLU's, which scale with the size the user has selected for the font. You can either scale your dialog explicitly, in pixels (bad solution, this will make your app look even worse on some configurations), or do your calculations in DLU's everywhere. Your second screenshot also seems to show that the child dialogs use a different font than those of the wizard. I'm not sure why that is, I guess it's something in the window styles you pass to the wizard when you create it.