QT GridLayout with QPushButton - qt

I emulate in my code the ribbon style. For this I use also QGridLayout.
But I have a Problem.
| colspan 3 QLineEdit|
|QLabel |QLineEdit |QLabel |QLineEdit |QLabel |QLineEdit|
|QLabel |QComboBox |QLabel |QLineEdit |
This looks like:
Now I need a QPushButton on the right hand side of the edit, so I try to add the button to the Grid.
myBootGridLayout->addWidget(btnSelBootImagePath,0,3);
I also tried 0,4 to make sure it is not a cell problem.
| colspan 3 QLineEdit| QPushButton|
|QLabel |QLineEdit |QLabel |QLineEdit |QLabel |QLineEdit|
|QLabel |QComboBox |QLabel |QLineEdit |
But after the QPushButton was added, the Grid looks like:
It looks like that the QPushButton kills the GRidLayout. I tried to set the button to fixed hight 16 and it do not help. Button is very wrong placed.
Finally I tried to set with CSS the margin and padding of the button to 0. Also do not work.
Is there a solution for this available? My goal is to have the QLineEdit and on the right hand a button to select a file.Both with the same height.
I have a couple of GRidLayouts there in my ui and all work fine, just the one with QPushButton not.
The complete Grid code is:
QGridLayout *myBootGridLayout = new QGridLayout(this);
QWidget * wdgBootFeatues = new QWidget(this);
imagePathBootEdit = new QLineEdit(this);
imagePathBootEdit->setReadOnly(true);
imagePathBootEdit->setMaximumWidth(300);
imagePathBootEdit->setMinimumWidth(300);
myBootGridLayout->addWidget(imagePathBootEdit,0,0,1,3);
//Problem
QPushButton *btnSelBootImagePath = new QPushButton(this);
btnSelBootImagePath->setText(tr("..."));
//btnSelBootImagePath->setMinimumWidth(16);
btnSelBootImagePath->setMaximumWidth(32);
//btnSelBootImagePath->setMinimumHeight(16);
btnSelBootImagePath->setMaximumHeight(16);
//connect(btnSelBootImagePath, SIGNAL(clicked()),this, SLOT(clickedSelBootImagePath()));
//btnSelBootImagePath->setStyleSheet("QPushButton{padding:0px;margin:0px;}");
myBootGridLayout->addWidget(btnSelBootImagePath,0,3);
QLabel *labelDeveloperID = new QLabel(this);
labelDeveloperID->setText(tr("Developer ID:"));
myBootGridLayout->addWidget(labelDeveloperID,1,0);
developerIDBootEdit = new QLineEdit(this);
developerIDBootEdit->setMinimumWidth(100);
developerIDBootEdit->setMaximumWidth(100);
//connect(developerIDBootEdit,SIGNAL(textChanged(QString)),this,SLOT(bootDeveloperIDchanged(QString)));
myBootGridLayout->addWidget(developerIDBootEdit,1,1);
QLabel *labelSectos = new QLabel(this);
labelSectos->setText(tr("Sectors:"));
myBootGridLayout->addWidget(labelSectos,1,2);
sectorsBootEdit = new QLineEdit(this);
sectorsBootEdit->setMinimumWidth(80);
sectorsBootEdit->setMaximumWidth(80);
//connect(sectorsBootEdit,SIGNAL(textChanged(QString)),this,SLOT(bootSectorschanged(QString)));
myBootGridLayout->addWidget(sectorsBootEdit,1,3);
QLabel *labelLoadSegment = new QLabel(this);
labelLoadSegment->setText(tr("Load segment:"));
myBootGridLayout->addWidget(labelLoadSegment,1,4);
loadSegmentBootEdit = new QLineEdit(this);
loadSegmentBootEdit->setMinimumWidth(80);
loadSegmentBootEdit->setMaximumWidth(80);
//connect(edBootLoadSegment,SIGNAL(textChanged(QString)),this,SLOT(bootLoadSegmentchanged(QString)));
myBootGridLayout->addWidget(loadSegmentBootEdit,1,5);
QLabel *labelEmulation = new QLabel(this);
labelEmulation->setText(tr("Emulation Type:"));
myBootGridLayout->addWidget(labelEmulation,2,0);
emulationTypeBootCombo = new QComboBox();
emulationTypeBootCombo->setMinimumWidth(100);
emulationTypeBootCombo->setMaximumWidth(100);
emulationTypeBootCombo->addItem(tr("No emulaton (NT/2000/XP)"));
emulationTypeBootCombo->addItem(tr("Floppy 1.2MB"));
emulationTypeBootCombo->addItem(tr("Floppy 1.44MB (95/98/ME)"));
emulationTypeBootCombo->addItem(tr("Floppy 2.88MB"));
emulationTypeBootCombo->addItem(tr("Hard Disk"));
//connect(emulationTypeBootCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(bootEmulationTypeChanged(int)));
myBootGridLayout->addWidget(emulationTypeBootCombo,2,1);
QLabel *labelPlatfom = new QLabel(this);
labelPlatfom->setText(tr("Platform:"));
myBootGridLayout->addWidget(labelPlatfom,2,2);
platformBootCombo = new QComboBox();
platformBootCombo->setMinimumWidth(100);
platformBootCombo->setMaximumWidth(100);
platformBootCombo->addItem(tr("x86 - 32"));
platformBootCombo->addItem(tr("x86 - 64"));
platformBootCombo->addItem(tr("PowerPC"));
platformBootCombo->addItem(tr("MAC"));
//connect(platformBootCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(bootPlatformChanged(int)));
myBootGridLayout->addWidget(platformBootCombo,2,3);
//Added for testing button problem
myBootGridLayout->setRowMinimumHeight(0,25);
myBootGridLayout->setRowMinimumHeight(1,25);
myBootGridLayout->setRowMinimumHeight(2,25);
myBootGridLayout->setContentsMargins(3,0,0,0);
//Add Lyout to ui
wdgBootFeatues->setLayout(myBootGridLayout);
wdgBootFeatues->setFixedHeight(80);
ui->ribbonTabWidget->addWidgetGroup("Boot Disc", "El-Torito", wdgBootFeatues);

Related

QT. Add new layout to QLayout

I create interface dynamically when Application is run.
1) I have QTabWidget with 4 predefined tabs. But i must show only 1 or 2 tabs, in case of user shoice. On StackOverflow i learned, that i must keep all tabs in collection to add and destroit it.
I have QHash: twInputMethodsTabs = new QHash< int, QPair<QWidget*, QString> >();
First argument = index; Second = Tab Widget; Third = Tab Widget Caption Text;
2) I fill the collection like this:
for(int i = 0; i < ui->twInputMethods->children().length(); i++)
{
twInputMethodsTabs->insert(i, QPair<QWidget*, QString>(ui->twInputMethods->widget(i), ui->twInputMethods->tabText(i)));
}
3) I add new widget in the tab like this:
twInputMethodsTabs->value(1).first->layout()->addWidget(cmbbCommands);
4) How can i add new layout to this widget? I want to do like this:
QHBoxLayout *hblParams =new QHBoxLayout();
twInputMethodsTabs->value(1).first->layout()->addLayout(hblParams);
But it does not work, because layout() returns QLayout which havent addLayout() function. How i can do this?
Or how can i should change architecture of code to do this?
In this following code you get a widget (.first) and then select that widget's layout ->layout() and then add a Widget to that layout ->addWidget().
twInputMethodsTabs->value(1).first->layout()->addWidget(cmbbCommands);
In the following code you get a widget (.first) and then select that widget's layout ->layout() and try to set the layout on the layout.
twInputMethodsTabs->value(1).first->layout()->addLayout(hblParams);
Replacing the QLayout
To set the layout on the parent widget, you need to drop the ->layout():
twInputMethodsTabs->value(1).first->addLayout(hblParams);
Note that since you are now adding an empty layout to the widget, any widgets current in the previous layout will be lost, so you may need to re-add the widgets to the layout.
Adding new QLayout inside existing QLayout
If you want to add a layout into the existing layout, you cannot do this directly. QLayout can only accept QWidget via .addWidget(). However, you can apply a layout to an empty QWidget() and then add that to the layout. For example:
QWidget *w = new QWidget();
w.addLayout(hblParams);
twInputMethodsTabs->value(1).first->layout()->addWidget(w);
An alternative is to set the layout on the QWidget to a layout that does support .addLayout() such as QHBoxLayout or QVBoxLayout. For example:
QVBoxLayout *l = new QVBoxLayout();
cmbbCommands.setLayout(l); // Now has a layout that supports .addLayout
twInputMethodsTabs->value(1).first->layout()->addWidget(cmbbCommands);
Now the following should work because ->layout() returns a QVBoxLayout:
QHBoxLayout *hblParams =new QHBoxLayout();
twInputMethodsTabs->value(1).first->layout()->addLayout(hblParams);
I Hope, I get what you want to do:
twInputMethodsTabs->value(1).first->layout()->addWidget(cmbbCommands);
QHBoxLayout *hblParams =new QHBoxLayout();
QWidget *w = new QWidget(twInputMethodsTabs->value(1).first);
twInputMethodsTabs->value(1).first->layout()->addWidget(w);
w->addLayout(hblParams);
I just wrote the code here, so it is untested. However it should give you an idea what I tried to explain in my comment.
Cutted from "working" application:
WidgetA::WidgetA(QWidget *parent) : QWidget(parent)
{
QVBoxLayout *pLayout = new QVBoxLayout();
buildWidget(pLayout);
this->setLayout(pLayout);
}
void WidgetA::buildWidget(QVBoxLayout *layout){
for(int i=0; i<4; ++i){
this->buildSegments(layout);
}
}
void WidgetA::buildSegments(QVBoxLayout *layout){
QHBoxLayout *pHbl = new QHBoxLayout();
QLabel *pSegmentSize = new QLabel(this);
pSegmentSize->setText(tr("Segment Size(1,N)"));
QSpinBox *pSegments = new QSpinBox(this);
pHbl->addWidget(pSegmentSize);
pHbl->addWidget(pSegments);
layout->addItem(pHbl);
}
Read this one: Widgets Tutorial - Nested Layouts

How to create a scrollable widget using QHBoxLayout and QScrollArea

I am trying to create a widget with scrollable using the QHBoxLayout and QScrollArea but i am unable to succeed to create a GUI,here is my code can any one please suggest me where i need to change.
Any help would be appreciated
mainLayout = new QHBoxLayout(this);
mainLayout->setSizeConstraint(QLayout::SetMaximumSize);
QCheckBox *sam_box = new QCheckBox(this);
sam_box->setText("HAIIIIIIIIIII");
sam_box->setFixedSize(10000,10000);
QCheckBox *so_box = new QCheckBox(this);
so_box->setText("HOWWWWWWWWWWWW");
so_box->setFixedSize(150000,15000);
mainLayout->addWidget(sam_box);
mainLayout->addWidget(so_box);
QScrollArea *scrollarea = new QScrollArea(this);
scrollarea->setBackgroundRole(QPalette::Shadow);
scrollarea->setFixedSize(700,500);
scrollarea->setLayout(mainLayout);`
Here is my output screen
Thanks in advance,
Rohith.G
One way to achieve this is by below logic
centralwidget() -> QHBoxLayout-> QScrollArea -> QWidget-> add the check box
The code flow & comments will explain the logic in detail.
QWidget *wgt = new QWidget();//new code
wgt->setGeometry(0,0,500,500); //new code
//QHBoxLayout mainLayout = new QHBoxLayout(this);
QHBoxLayout *mainLayout = new QHBoxLayout(this->centralWidget());
mainLayout->setSizeConstraint(QLayout::SetMaximumSize);
//QCheckBox *sam_box = new QCheckBox(this);
QCheckBox *sam_box = new QCheckBox(wgt);
sam_box->setText("HAIIIIIIIIIII");
//sam_box->setFixedSize(10000,10000);
sam_box->setFixedSize(200,200); //make it small for quick testing
//QCheckBox *so_box = new QCheckBox(this);
QCheckBox *so_box = new QCheckBox(wgt);
so_box->setText("HOWWWWWWWWWWWW");
//so_box->setFixedSize(150000,15000);
so_box->setFixedSize(150,150); //make it small for quick testing
//mainLayout->addWidget(sam_box);
//mainLayout->addWidget(so_box);
QScrollArea *scrollarea = new QScrollArea();
scrollarea->setBackgroundRole(QPalette::Shadow);
//scrollarea->setFixedSize(700,500); //scroll area cant be resized
mainLayout->addWidget(scrollarea); //new code
//scrollarea->setLayout(mainLayout);
scrollarea->setWidget(wgt);
Below code will generate 30 QCheckBoxes added each ten in a vertical box layout and all the vertical layout in a horizontal box layout
QScrollArea *scrl = new QScrollArea();
scrl->setGeometry(0,0,300,300);
QWidget *wgtMain = new QWidget();
QHBoxLayout *hboxMain = new QHBoxLayout(wgtMain);
for(int iCount=0;iCount<3;iCount++){
QWidget *wgtSub = new QWidget();
QVBoxLayout *vboxSub = new QVBoxLayout(wgtSub);
for(int jCount=0;jCount<10;jCount++){
QCheckBox *chk = new QCheckBox("Check Box " + QString::number(jCount+1) + " of " + QString::number(iCount+1));
vboxSub->addWidget(chk);
}
hboxMain->addWidget(wgtSub);
}
scrl->setWidget(wgtMain);
scrl->show();

QToolButton with text: Overwrite minimal height to minic regular button height

I am displaying QToolButtons with icon plus text (Qt::ToolButtonTextBesideIcon) outside of a tool bar. Each button has a QAction associated with it which determines the used icon and the displayed text. All those buttons are placed inside a QGridLayout. So far so good.
Unfortunately, it looks like that as soon as you add a QAction to a QToolButton, Qt automatically decides to shrink it down to the minimal size, which is not what I want given my QGridLayout. I used the following lines to correct that behavior in the horizontal dimension:
QToolButton* pButton = new QToolButton(0);
pButton->addDefaultAction(pAction);
pButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
pButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
However that still leaves my button with a smaller height than a regular (push) button. I already tried various other size policies without success.
How to best solve this issue? Is there a reliable way to determine the "normal" button height?
One idea I had was to create a regular dummy button, place it in the same layout and then read its size. This size could then be applied to my QToolButton and the dummy button would be destroyed again. Is there a more elegant / reliable way?
I do not understand what do you want to achieve.
Difference between QPushButton and QToolButton is that, QToolButton has implemented PopupMenu ( can be done easily for QPushButton also )
As I understand visual difference is only small arrow in lower right corner of QToolButton, when you use added QActions to QToolButton
This arrow is for me only difference between QToolButton and QPushButton. But maybe I am missing something.
From your examples ( QToolButton with icon + text: How to center both? )
it does not look like you want to use that popup feature. Thats why I do not understand, why to use QToolButton instead of QPushButtons.
In this example shows:
1) Same height of QToolButton and QPushButton
2) PopuMenu for QPushButton
As for me, I do not understand why to use QToolButton and try to make it look like QPushButton when it is simple to use QPushButton as QToolButton
#include <QtGui>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Prepare layout
QMainWindow *window = new QMainWindow;
QWidget *centralWidget = new QWidget(window);
QGridLayout *grid = new QGridLayout(centralWidget);
QTextEdit *textEdit = new QTextEdit();
window->setCentralWidget(centralWidget);
QAction *toolAction = new QAction(window->style()->standardIcon(QStyle::SP_MediaPlay), "ToolButton", window);
QObject::connect(toolAction, &QAction::triggered, [=]() {
qDebug() << "action";
});
QPushButton *pushButton = new QPushButton(toolAction->icon(), "PushButton1", window);
pushButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QPushButton *pushButton2 = new QPushButton(toolAction->icon(), "PushButton2", window);
pushButton2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QPushButton *pushButton3 = new QPushButton(toolAction->icon(), "PushButton2", window);
pushButton3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QObject::connect(pushButton3, &QPushButton::released, [window, pushButton3, toolAction](){
QMenu menu;
menu.addAction(toolAction);
QPoint pos = window->mapToGlobal(pushButton3->pos());
pos += QPoint(0, pushButton3->height());
menu.exec(pos);
});
QObject::connect(pushButton, SIGNAL(pressed()), toolAction, SLOT(trigger()));
QObject::connect(pushButton2, SIGNAL(pressed()), toolAction, SLOT(trigger()));
QToolButton *toolButton = new QToolButton(window);
toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolButton->setText("Popup action");
toolButton->addAction(toolAction);
toolButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QToolButton *toolButton2 = new QToolButton(window);
toolButton2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolButton2->setDefaultAction(toolAction);
toolButton2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
toolButton->setMaximumHeight(pushButton->sizeHint().height());
toolButton->setMinimumHeight(pushButton->sizeHint().height());
toolButton2->setMaximumHeight(pushButton->sizeHint().height());
toolButton2->setMinimumHeight(pushButton->sizeHint().height());
grid->addWidget(textEdit ,0,0,1,2);
grid->addWidget(toolButton ,1,0,1,1);
grid->addWidget(pushButton ,1,1,1,1);
grid->addWidget(toolButton2 ,2,0,1,1);
grid->addWidget(pushButton2 ,2,1,1,1);
grid->addWidget(pushButton3 ,3,0,1,2);
window->show();
return a.exec();
}

Add widgets to a ScrollArea

I am creating a window dimanica to the downloads list.
But the scrollbar does not work and the "widgets children" are "cut".
Where can I be wrong? Thanks.
Source:
QWidget *central = new QWidget;
QScrollArea *scroll = new QScrollArea;
QVBoxLayout *layout = new QVBoxLayout(scroll);
scroll->setWidget(central);
scroll->setWidgetResizable(true);
int i=0;
while(i<10){
QWidget *p1 = new QWidget;
QHBoxLayout *hl = new QHBoxLayout(p1);
QLabel *label1 = new QLabel("test");
QLabel *label2 = new QLabel("0%");
hl->addWidget(label1);
hl->addWidget(label2);
layout->addWidget(p1);
i++;
}
QMainWindow *w = new QMainWindow;
w->setGeometry(50,50,480,320);
w->setCentralWidget(scroll);
w->show();
Found your mistake, you should set layout to widget central not to scroll:
QWidget *central = new QWidget;
QScrollArea *scroll = new QScrollArea;
QVBoxLayout *layout = new QVBoxLayout(central);
scroll->setWidget(central);
scroll->setWidgetResizable(true);
EDITED:
Your labels already take all available space, if you noticed, label1 begins at left border ends in the middle, where label2 starts and ends at the right border. If I understood you correctly, you want label1 to take all the space available, while label2 with percents to take only what space is needed, no more?
Read about QSizePolicy class and use setSizePolicy() on your labels. Try to insert this line right after label2 declaration:
QLabel *label2 = new QLabel("0%");
label2->setSizePolicy(QSizePolicy::QSizePolicy::Maximum,QSizePolicy::Maximum);
And add line layout->addStretch(); right before QMainWindow *w = new QMainWindow;

QTextBrowser Resizing on a QGridLayout

I have a QGridLayout which contains all the layout for my class. Until there everything goes fine.
I added a QSplitter for other things and then at the bottom (After the splitter) i want the QTextBrowser to be. Fine, works. But i want to resize, i meant i want to make the QTextBrowser smaller. But resize doesn't work.
QGridLayout *layout = new QGridLayout(this);
QSplitter *splitter = new QSplitter(Qt::Horizontal);
text1 = new QPlainTextEdit();
text2 = new QPlainTextEdit();
splitter->addWidget(text1);
splitter->addWidget(text2);
text1->resize(800, this->height());
layout->addWidget(splitter, 1, 0);
browser = new QTextBrowser();
browser->resize(1, 1);
layout->addWidget(browser, 2, 0);
setLayout(layout);
Actually resizing the text1 works fine, but i can't make the QTextBrowser resize fine. Any idea?

Resources