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;}");
Related
I'm trying to create a QScrollArea in a QTabWidget.
Versions :
Qt 5.15.0
Qt creator 4.12.4
MSVC2019 64 bits
First of all, I've created the QTabWidget :
tabWidget = new QTabWidget(this);
tabWidget->setGeometry(10, 15, 1200, 665);
tabWidget->setStyleSheet("font-size : 15px");
tab1Content = new QWidget(tabWidget); tabWidget->addTab(tab1Content, "tab1");
tab2Content = new QWidget(tabWidget); tabWidget->addTab(tab2Content, "tab2");
tab3Content = new QWidget(tabWidget); tabWidget->addTab(tab3Content, "tab3");
tab4Content = new QWidget(tabWidget); tabWidget->addTab(tab4Content, "tab4");
I can add
tabWidget->setEnable(true);
And for all tabs, 0 <= i < tabWidget.count
tabWidget->setTabEnabled(i, true);
Click to change tab doesn't work : https://i.stack.imgur.com/8r1Jg.png
Strange thing : color looks like enabled but i can only change tabs with ← → and when i lost tabWidget focus by clicking on an other thing outside the tabWidget, i can't regain focus.
So i've created temporary button to change tabs and linked to tabWidget like that :
connect(changeTab, &QPushButton::clicked, [&]() {onChangeTab();});
void MainWindow::onChangeTab() {
tabWidget->setCurrentIndex(tabWidget->currentIndex() >= tabWidget->count() - 1 ? 0 : tabWidget->currentIndex() + 1);
}
It works well.
Thus, I've start to create the QScrollArea :
First, it doesn't work, so I've tried to found sth on internet :
QScrollArea not working as expected with QWidget and QVBoxLayout
My result : https://i.stack.imgur.com/jvVol.png
I cannot click on a single button and i can't scroll...
And if i try to force scroll like this, it doesn't scroll
scrollArea->scroll(0, 50);
Last thing, there isn't infinite loop or dead lock things because all things around this cursed tabWidget and scroll Area work perfectly.
I don't know why these objects "don't answer" if somedoby had this kind of experiment could you help me please ?
Thank you very much in advance.
try this code
#include "widget.h"
#include<QTabWidget>
#include<QLabel>
#include<QVBoxLayout>
#include<QScrollArea>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget->setGeometry(10, 15, 1200, 665);
tabWidget->setStyleSheet("font-size : 15px");
QWidget * tab1Content = new QWidget;
//preparing tab1content ( e.g.)
QVBoxLayout * verticalLayout = new QVBoxLayout;
// adding items to vertical layout
for(int i=0;i<100;i++)
verticalLayout->addWidget(new QLabel(QString::number(i)));
// set this vertical layout inside tab1content
tab1Content->setLayout(verticalLayout);
// create new scroll area ...
QScrollArea * scroll = new QScrollArea;
// ... and add tab1content in scroll area
scroll->setWidget(tab1Content);
// and finally add scroll area inside tabwidget
tabWidget->addTab(scroll,"tab1");
QWidget * tab2Content = new QWidget; tabWidget->addTab(tab2Content, "tab2");
QWidget * tab3Content = new QWidget; tabWidget->addTab(tab3Content, "tab3");
QWidget * tab4Content = new QWidget; tabWidget->addTab(tab4Content, "tab4");
}
Widget::~Widget()
{
}
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?
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().
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."
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 ?