QToolbutton text overlapping menu button - c++

When having a long text applied to a QToolbutton it overlaps the menu button for the right click menu like the following:
The code for the Button
TestWindow::TestWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TestWindow)
{
ui->setupUi(this);
QMenu *menu = new QMenu();
QAction *testAction = new QAction(QLatin1String("testAction"), this);
menu->addAction(testAction);
ui->toolButton->setMenu(menu);
ui->toolButton->setText(QLatin1String("This is a long Text that should not overlap"));
ui->toolButton->setPopupMode(QToolButton::InstantPopup);
}
The toolbutton is just part of a horizontal layout of the central widget.
Is there an easy way to prevent this by e.g. cropping the text?

Related

QTreeWidget with non-native scroll bar when background changed

I need a QTreeWidget with transparent background so it has the same color as the native light-gray window background. This works fine by setting the background to transparent.
The problem is that if I do this, the scroll becomes non-native looking. The default background of QTreeWidget is "white" and if I don't change it, the scroll bar does look native. However, if I change the background to "transparent", the scrollbar looses its native appearance.
To demonstrate this, I put two QTreeWidgets next to each other, one with the default white background showing the native scroll bar and one with the background changed to transparent, showing a non-native scroll bar: screenshot
Here is the code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QHBoxLayout* layout = new QHBoxLayout(this);
ui->centralWidget->setLayout(layout);
QTreeWidget* tree1 = new QTreeWidget();
QTreeWidget* tree2 = new QTreeWidget();
layout->addWidget(tree1);
layout->addWidget(tree2);
// add ten items to each tree widget
for(int i=0; i<10; i++){
QString item_text = "item " + QString::number(i);
QTreeWidgetItem* item1 = new QTreeWidgetItem();
item1->setText(0, item_text);
tree1->addTopLevelItem(item1);
QTreeWidgetItem* item2 = new QTreeWidgetItem();
item2->setText(0, item_text);
tree2->addTopLevelItem(item2);
}
// change the background color of tree2 to the window color
// this leads to a non native scroll bar for tree2
tree2->setStyleSheet("background-color: transparent;");
}
How can I have the transparent background an still keep the native scroll bar?
I finally found the solution. I need to restrict the definition of the background-color to the QTreeWidget:
tree2->setStyleSheet("QTreeWidget {background-color: transparent;}");

qt button added on menu bar

I'm trying to add buttons to a vertical layout in QT.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
mRootLayout = new QVBoxLayout(this);
setLayout(mRootLayout);
mRootLayout->addWidget(new QPushButton("Button1", this));
mRootLayout->addWidget(new QPushButton("Button2", this));
}
I have 2 problems
1. The buttons are created on top of the menu bar
2. The buttons are not one under the other one.
I'm using a QVBoxLayout.
I think code must be change to:
mRootLayout = new QVBoxLayout(ui->centralWidget);
mRootLayout->addWidget(new QPushButton("Button1", this));
mRootLayout->addWidget(new QPushButton("Button2", this));
It's not necessary do setLayout().

How to resize an expandable dialog?

I am trying to create an expandable Qt dialog application. The main layout is a QVBoxLayout. The top part has two views and a QPushButtonbutton. Clicking button will unfold the bottom widget which is initially hidden. In the bottom widget, there is another push button, which could fold (hide) the bottom widget. When the bottom widget fold/unfold, I expect the size of the dialog size to change as well.
But for some reason, the dialog size only increases when the bottom widget is unfolded. And never shrink back to (200, 100). Is there anything I missed?
Environment: Qt Creator 3.6.1; Based on Qt5.6.0 (MSVC2013 32bit); build on Mar 14 2016; revision d502727b2c
The code I am using :
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QTreeView *tree = new QTreeView;
QTableView *table = new QTableView;
QPushButton *button_show = new QPushButton;
button_show->setText(tr("Show hidden panel"));
QHBoxLayout *layout_top = new QHBoxLayout;
layout_top->addWidget(tree);
layout_top->addWidget(table);
layout_top->addWidget(button_show);
QHBoxLayout *layout_bottom = new QHBoxLayout;
QTextEdit *editor = new QTextEdit;
QPushButton *button_hide = new QPushButton;
button_hide->setText(tr("Hide the bottom panel"));
g_pEditor = editor;
layout_bottom->addWidget(editor);
layout_bottom->addWidget(button_hide);
QWidget *panel = new QWidget;
panel->setLayout(layout_bottom);
QVBoxLayout *layout_main = new QVBoxLayout;
layout_main->addLayout(layout_top);
layout_main->addWidget(panel);
setLayout(layout_main);
panel->hide();
connect(button_show, &QPushButton::clicked
, panel
, [=]()
{
panel->setVisible(true);
button_show->setEnabled(false);
resize(200, 200);// not really working, the dialog size is able to increase without calling resize()
});
connect(button_hide, &QPushButton::clicked, panel, [=]()
{
panel->hide();
button_show->setEnabled(true);
resize(200,100);// does not shrink the dialog size*
});
resize(200,100);
}
Thanks for your help :)
Your should try setFixedSize(w, h) instead. This sets both, the minimum and the maximum size to (w, h). "This will override the default size constraints set by QLayout."

QDialog widget with null parent always visible

I am working with qt 3.3. I need to make QDialog widget with null parent always visible not stays on top (WStyle_StaysOnTop) because this flag block access for main application. I need on screen keyboard functionality for my QDialog widget.
I hope I correctly understood the question. Here is a minimal example of what u want.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDialog *dia = new QDialog(this);
//Set the windowflags
dia->setWindowFlags(dia->windowFlags() | Qt::Tool);
dia->show();
QWidget *central = new QWidget;
QHBoxLayout *mainLayout = new QHBoxLayout;
QLineEdit *edit = new QLineEdit;
//Add sample QLineEdit to test the input focus for mainwindow
mainLayout->addWidget(edit);
central->setLayout(mainLayout);
setCentralWidget(central);
}
edit:
If you want to be able to minimize and maximize the dialog in question from systray you have to create the QSystrayIcon and context menu for it:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDialog *dia = new QDialog(this);
dia->setWindowFlags(dia->windowFlags() | Qt::Tool);
dia->show();
QWidget *central = new QWidget;
QHBoxLayout *mainLayout = new QHBoxLayout;
QLineEdit *edit = new QLineEdit;
mainLayout->addWidget(edit);
central->setLayout(mainLayout);
setCentralWidget(central);
//Create the icon for systray
//NOTE this icon is application wide
QSystemTrayIcon *icon = new QSystemTrayIcon(QIcon(QPixmap("/usr/share/icons/oxygen/22x22/status/user-away.png")), dia);
icon->setVisible(true);
//Create context menu to manipulate the dialog
QMenu *contextMenu = new QMenu;
QAction *minimizeDialog = contextMenu->addAction("Minimize dialog");
QAction *restoreDialog = contextMenu->addAction("Restore dialog");
connect(minimizeDialog, SIGNAL(triggered()), dia, SLOT(hide()));
connect(restoreDialog, SIGNAL(triggered()), dia, SLOT(show()));
//Add it to the icon
icon->setContextMenu(contextMenu);
}

How to update a docking corner?

I am making a application by Qt, that has a central widget, right dock widget and left dock widget. Their sizes are fixed.
They are displayed, but there a blank space between the central widget and the right widget when the left dock widget is floating.
https://twitter.com/#!/hizz_GI/status/155768124321435648/photo/1
Will you please tell me the way to remove the blank space?
I appreciate you taking the time to respond to my question.
code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
textEdit = new QTextEdit;
textEdit->setFixedSize(100, 150);
setCentralWidget(textEdit);
creatDocks();
layout()->setSizeConstraint(QLayout::SetFixedSize);
}
void MainWindow::creatDocks()
{
leftTextEdit = new QTextEdit;
rightTextEdit = new QTextEdit;
leftDock = new QDockWidget(tr("Left Dock Widget"));
rightDock = new QDockWidget(tr("Right Dock Widget"));
leftDock->setFixedSize(100, 150);
leftDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
leftDock->setWidget(leftTextEdit);
rightDock->setFixedSize(150, 150);
rightDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
rightDock->setWidget(rightTextEdit);
addDockWidget(Qt::LeftDockWidgetArea, leftDock);
addDockWidget(Qt::RightDockWidgetArea, rightDock);
}
I found a solving.
It is a way that calls menuWidget()->adjustSize() and adjustSize() when paintEvent of MainWindow without textEdit->setFixedSize() and layout()->setSizeConstraint().
But it is expensive. What time is the proper call?
And is this appropriate?
Thanks.
Probably, since you set all the 3 widget to have a fixed size, when your left widget is floating, the central widget correctly go to the left, but since it and the right widget have a fixed size, they where not resized to fill the space in the middle.
Have you the same problem also when the central or right widget are floating instead of the left widget ?