PDA

View Full Version : Sizing menubar



Radek
7th July 2013, 17:51
I am trying to create a menubar for a frame window. I can achieve a menubar but with items glued together, something like "FileEditViewHelp" (4 menus, all 4 menus work). Searching for a fix, I have found out that I need to set style sheets for File and other menus, namely set padding. Thus I set



padding: 5 px 0px 5px 20px;


for all menus but I was ignored. (Edit: The "1." was added by the forum, it is not in the stylesheet.)The menus continue being glued together. No padding anywhere. What to do? More precisely, what to do from a Qt Designer? I would like to keep the "GUI look" in the .ui file and not in the code.

Another question is about menu icons. It seems to be possible to add icons to the menus (I mean texts on the menubar). I created a folder, put icons (.png) into it, created a .qrc and specified icons for the menus (choose resource). The images were found, they were displayed in the resource editor as well as in the Designer data for the menus. But they weren't displayed at application run. I was ignored again. Is it possible to add icons to menus?

Debian Wheezy, KDE, Qt 4.8.2

ChrisW67
8th July 2013, 00:42
Why not use QMenuBar to build the menu bar and get standard spacing?

If you want to proceed down the path you are on: the padding is inside the widget, perhaps you wanted the margin.


Is it possible to add icons to menus?
Yes, the menu will display the icon associated with the action. Since we cannot see what you have actually done we cannot diagnose the problem.

Radek
8th July 2013, 05:45
I am trying to create the menubar by means of Qt Designer. I am following the standard way "Type here" etc. Naturally, I am creating a QMenuBar. Here is a part of ui_myframe.h resulting from myframe.ui



menubar = new QMenuBar(MyFrame);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 800, 25));
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(menubar->sizePolicy().hasHeightForWidth());
menubar->setSizePolicy(sizePolicy);
menubar->setMinimumSize(QSize(100, 25));
menuFile = new QMenu(menubar);
menuFile->setObjectName(QString::fromUtf8("menuFile"));
menuFile->setGeometry(QRect(259, 144, 122, 130));
QSizePolicy sizePolicy1(QSizePolicy::Preferred, QSizePolicy::Preferred);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(menuFile->sizePolicy().hasHeightForWidth());
menuFile->setSizePolicy(sizePolicy1);
menuFile->setMinimumSize(QSize(0, 0));
menuFile->setFont(font);
menuFile->setStyleSheet(QString::fromUtf8("padding: 5px 20px 5px 20px"));
QIcon icon;
icon.addFile(QString::fromUtf8(":/icon/rc/file.png"), QSize(), QIcon::Normal, QIcon::Off);
menuFile->setIcon(icon);


Other menus follow. The result is "FileEditViewHelp", no icons.

ChrisW67
8th July 2013, 06:19
Does the Menus Example work correctly for you? The default behaviour of QMenuBar is to neatly space out the QMenu headings. What are you doing that is not standard?

While you can set an icon on the QMenu objects in Designer the QMenuBar does not render them under Qt 4.8 AFAICT and does a poor job under 5.1 on my Linux box. First things-first though.

Radek
8th July 2013, 07:21
The culprit seems to be the icon. I have found the following:

(a) As long as I do not attach the icon, the headings are separated acceptably. I can increase the gaps by blanks before (or after) the heading texts. Not very clean but it works.
(b) Once I attach an icon, the size of the bounding box of the heading shrinks fo a few pixels (even if it was wider before) and the headings become "FileEditView". The icon is ignored and there seems to be no way to make he bounding box of the heading wider. Trying a long text in the heading does not help: you will see only a few letters in the current minibox.

Therefore, no icons in menus.