qt5 Combine QGroupBox and QPushButton - c++

I'm trying to create a button with a drop-down list but at the same time, I want the current selected text to be able to activated like a QPushButton. Here are a couple of pics of what I'm looking for, before and after. (Line from MS Paint)
I want to have it so if the user clicks to the left of the line, it will resend the signal of whatever item is shown. If the user clicks the drop-down arrow, that list will show up and from then on will act like a normal Group Box until another Item is selected.
http://imgur.com/a/tFoLv

You can use a QToolButton and a QMenu for it:
QToolButton *btn = new QToolButton();
btn->setPopupMode(QToolButton::MenuButtonPopup);
QMenu *btnMenu = new QMenu;
QAction *action1 = btnMenu->addAction("action1");
QAction *action2 = btnMenu->addAction("action2");
btn->setMenu(btnMenu);
connect(btn, SIGNAL(clicked()), this, SLOT(btnSlot()));
connect(action1, SIGNAL(triggered()), this, SLOT(action1Slot()));
connect(action2, SIGNAL(triggered()), this, SLOT(action1Slot()));

Related

QT add Label to a page of stacked widget

I am trying to create a UI in QT C++. I have a stacked widget and it has many pages in it. I want to add a new label to the 5th page(page5) of the stacked widget. It should be visible in the UI. How can i add a dynamically created label to that page?
QLabel *label = new QLabel(this);
label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
label->setText("first line\nsecond line");
label->setAlignment(Qt::AlignBottom | Qt::AlignRight);
//Here is how to change position:
label->setGeometry(QRectF(10,10,30,80));
this code might create the label but how to show this in the 5th page of my stacked widget.
Thank you and regards.

Customize QMenu added to a QMenuBar

So I try, with Qt, to bold the fourth Menu Title "Test" in a QMenuBar (see picture below).
QMenuBar* pQMenubar = new QMenuBar();
pQMenubar->addMenu(new QMenu("Fichier")); //Some QMenu, not important
pQMenubar->addMenu(new QMenu("Windows")); //Some QMenu, not important
pQMenubar->addMenu(new QMenu("Tools")); //Some QMenu, not important
QMenu* pQMenuTest = new QMenu("Test"); // The Menu I wanna bold
pQMenubar->addMenu(pQMenuTest);
//There is some action added to each QMenu, but I do not want them bold
To achieve that, I've tried by setting StyleSheet or font to the QMenuBar or QMenu("test")
Set the font (setBold(true)) or StyleSheet(font-weighted: bold) to the QMenu do not work, it only set actions contains in the QMenu in bold.
But when I change styleSheet or Font of the QMenuBar, all QMenu and QAction are bold, so I've tried this way.
I've tried to "filter" the object affected by the style sheet :
//Try 1
pQMenuBar.setStyleSheet(QMenu {font-weight: bold}); //Nothing is bold
//Try 2
pQMenuTest.setObjectName("Test");
pQMenuBar.setStyleSheet(QMenu#Test {font-weight: bold}); //Nothing is bold
//Try 3
pQMenuTest.setObjectName("Test");
pQMenuBar.setStyleSheet(#Test {font-weight: bold}); //Nothing is bold
//Try 4, just to check if it's works
pQMenuTest.setObjectName("Test");
pQMenuBar.setStyleSheet(QWidget {font-weight: bold}); //Evrything is bold
I've started to suspect the QMenuBar.addMenu(QMenu) do not really use the QMenu for the "display" and so filter by QMenu or the objectname do not work. (I've tried to filter with QAction, but nothing is bold too).

How to remove focus from MyGUI Widget?

I have a MyGUI::ButtonPtr and on click of this widget I am showing a QWidget. By default focus is on QWdiget but it seems that MyGUI widget also has focus which is creating few issues for me.
myButton = widPtr.at(0)->findWidget("settings")->castType<MyGUI::Button>();
myButton->eventMouseButtonClick += MyGUI::newDelegate(this, settingsClicked);
addToolTip(myButton, "Tooltip text");
void addToolTip(MyGUI::Widget *widget, QString toolTipLabel)
{
widget->eventToolTip += MyGUI::newDelegate(this, notifyTooltipEvent);
widget->setNeedToolTip(true);
widget->setUserString("tooltip", toolTipLabel.toStdString());
}
This tooltip should only be displayed on mouseover but it is visible also when button is clicked and QWidget is open which is incorrect. Reason for this seems to me that MyGUI button still has focus due to which tooltip is being displayed. I wish to remove this focus from MyGUI button.
This worked for me.
MyGUI::InputManager::getInstancePtr()->injectMouseRelease(0, 0, MyGUI::MouseButton::Button0);

QLabel & QComboBox setFont does NOT work

I'm using Qt Framework to build an App supporting multiple languages.
The default font is loaded from StyleSheet.
I override paintEvent() method, and setFont() method works OK for all widgets except for QLabel and QComboBox.
For QComboBox, the selected item has the correct font, the but the dropdown list items are using the default font. The Qt manual says setFont will set the font for both the comboBox button and the comboBox popup list to font.
Anyone happens to see this problem and have an idea to fix that? Thanks.
Answer is so long, because I wrote different approaches, choose the best for you.
Try to do next:
Create QListView, customize it (with stylesheet for example)
Set model with your data and set view to QComboBox with special methods:
setModel() and setView()
http://qt-project.org/doc/qt-4.8/qcombobox.html#setView
setStyleSheet("font-family: Arial;font-style: normal;font-size: 12pt");
For label you can use stylesheet too, setFont or just set HTML code with suitable font:
QFont f( "Arial", 14, QFont::Bold);
label->setFont(f);
With ComboBox you can use this for example:
QStringList stringList;
stringList << "#hello" << "#quit" << "#bye";
QStringListModel *mdl = new QStringListModel(stringList);
QFont comboFont("Arial",16,-1,true);
QListView *vw = new QListView;
vw->setFont(comboFont);
ui->comboBox->setModel(mdl);
ui->comboBox->setView(vw);
But it will install font to your data in popup menu, not in the header, so you can use also next:
QFont comboFont("Arial",16,-1,true);
for(int i = 0; i< ui->comboBox->count(); i++)
{
ui->comboBox->setItemData(i,QVariant(comboFont),Qt::FontRole);
}
ui->comboBox->setFont(comboFont);
Wityh this code snippet you'll get popup menu and header with this font and you don't need create models and views.
My dear, it is enough to do hereunder:
ui->CboxOpisBaza->lineEdit()->setFont(QFont("MS Shell Dlg 2", 12));

Internal QWidgets of QTabBar's tab?

As a follow up of " Hide label text for Qt tabs without setting text to empty string " :
Can I directly access the widgets within the tabs of the QTabBar. I do not mean the corresponding widget which is shown when I select a tab, but the tab's widgets (so in the screenshot below the log label and log icon).
I have tried QTabBar::findChildren, but with no success. Any idea?
QTabBar header sections are not actually widgets. They are drawn by QStylePainter inside QTabBar::paintEvent. Thus you can't get access to them.
As a workaround you can add a tab with an empty text and set a custom widget to it:
QTabBar *bar = new QTabBar;
bar->addTab("");
QLabel *label = new QLabel("my label");
bar->setTabButton(0, QTabBar::LeftSide, label);