Adding multiple QDockWidget's - c++

I am able to add QDockWidgets as follows:
QDW1 QDW2;
QDW3 QDW4;
by using the code
QDockWidget *dwidget = new QDockWidget(tr("QDW1"), this);
addDockWidget(Qt::LeftDockWidgetArea, dwidget);
dwidget = new QDockWidget(tr("QDW2"), this);
dwidget->show();
addDockWidget(Qt::LeftDockWidgetArea, dwidget);
QDockWidget *dwidget2 = new QDockWidget(tr("QDW2"), this);
addDockWidget(Qt::RighttDockWidgetArea, dwidget2);
dwidget2 = new QDockWidget(tr("QDW4"), this);
dwidget2->show();
addDockWidget(Qt::RighttDockWidgetArea, dwidget2);
Now I want to add as follows:
I want to add total of 6 QDockWidgets
QDW1 QDW2 QDW3;
QDW4 QDW5 QDW6;
Can you please give me an idea how to add the dock widgets in this 2X3 format?
Thank you for the help.
HBS

This should do the trick :
QDockWidget *dwidget = new QDockWidget(tr("QDW1"), this);
addDockWidget(Qt::RightDockWidgetArea, dwidget);
QDockWidget *dwidget2 = new QDockWidget(tr("QDW2"), this);
splitDockWidget(dwidget, dwidget2, Qt::Horizontal);
QDockWidget *dwidget3 = new QDockWidget(tr("QDW3"), this);
addDockWidget(Qt::RightDockWidgetArea, dwidget3);
QDockWidget *dwidget4 = new QDockWidget(tr("QDW4"), this);
splitDockWidget(dwidget3, dwidget4, Qt::Horizontal);
QDockWidget *dwidget5 = new QDockWidget(tr("QDW5"), this);
addDockWidget(Qt::RightDockWidgetArea, dwidget5);
QDockWidget *dwidget6 = new QDockWidget(tr("QDW6"), this);
splitDockWidget(dwidget5, dwidget6, Qt::Horizontal);

This is a pretty easy answer, maybe I am missing something - couldn't you just add three each to the top and bottom DockWidgetArea?
How does your main widget / layout look like? Usually dockWidgets are not intended as the sole content, no?

Related

How to manage layout on QDockWidget?

I'm getting started with Qt and I have a project for my school. I want to make an interface that read a database of space-stuff and displays them.
Until now I can display the table in a list, and my actual goal is to show the details of the object when it is double clicked.
To do so I tried to open a second dock whenever the object is double clicked, and tried to make a layout inside the second dock to display properly the details.
The program successfuly open a new dock, but the widget are way too small and all packed in the upper left corner of the dock:
picture of the bug:
I tried to do not use layout but only setWidget with QDockWidget, but it does only display the last widget.
Here is my code for the layout:
dock1 = new QDockWidget(tr("Caractéristiques de l'objet : "), this);
dock1->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
QVBoxLayout* layout = new QVBoxLayout();
Nom_Objets = new QLineEdit(dock1);
layout->addWidget(Nom_Objets);
//dock1->setWidget(Nom_Objets);
Categorie_Objets = new QLineEdit(dock1);
layout->addWidget(Categorie_Objets);
//dock1->setWidget(Categorie_Objets);
Description_Objets = new QTextEdit(dock1);
layout->addWidget(Description_Objets);
//dock1->setWidget(Description_Objets);
setLayout(layout);
addDockWidget(Qt::RightDockWidgetArea, dock1);
If you want to add several widgets to a QDockWidget then you must use a QWidget as a container:
dock1 = new QDockWidget(tr("Caractéristiques de l'objet : "), this);
dock1->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea, dock1);
QWidget* container = new QWidget;
dock1->setWidget(container);
QVBoxLayout* layout = new QVBoxLayout(container);
Nom_Objets = new QLineEdit;
Categorie_Objets = new QLineEdit;
Description_Objets = new QTextEdit;
layout->addWidget(Nom_Objets);
layout->addWidget(Categorie_Objets);
layout->addWidget(Description_Objets);

QTableWidget selection rectangle does not disappear even after editing is finished

I am implementing a QTableWidget, the selection rectangle seems not to be disappearing even after I finish editing and change my selection to other cells.
Below is a screenshot of the QTableWidget.
Below is the code for constructing the tablewidget, rows are added dynamically via a QPushButton:
{
setObjectName(obj_name);
layout = new QVBoxLayout();
table = new QTableWidget(this);
table->verticalHeader()->setVisible(false);
table->verticalHeader()->setDefaultSectionSize(20);
table->setFixedWidth(180);
table->setColumnCount(3);
table->setColumnWidth(0,75);
table->setColumnWidth(1, 75);
table->setColumnWidth(2, 25);
QStringList header = { "Tag", "Threshold" ,""};
table->setHorizontalHeaderLabels(header);
table->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
add = new QPushButton("+", this);
add->setObjectName("btn_threshold_add");
layout->addWidget(table);
layout->addWidget(add);
setLayout(layout);
connect(add, SIGNAL(clicked()), this, SLOT(add_row()));
}
Below is the code for add_row() SLOT, triggers when user clicked the add button:
void TagThresholdWidget::add_row()
{
int row = table->rowCount();
QPushButton *del = new QPushButton("-", table);
table->insertRow(row);
table->setCellWidget(row, 2, del);
connect(del, SIGNAL(clicked()), this, SLOT(remove_row()));
}
Anyone has any clue to how to solve this problem? Its seems like a Qt graphical paint bug to me

Add widgets into a QTabWidget

Can I add some widgets like QLabel and QPushButton into a QTabWidget?
Actually, I want to do something like this:
I'm using C++ and Qt.
Thanks
It's possible, just use QTabWidget::setCornerWidget.
Quick example:
QWidget* pTabCornerWidget = new QWidget(this);
QLabel* pLabelTime = new QLabel(pTabCornerWidget);
pLabelTime->setText("10:22:20");
QPushButton* pButton = new QPushButton(pTabCornerWidget);
pButton->setText("?");
pButton->setMaximumSize(QSize(25, 25));
QHBoxLayout* pHLayout = new QHBoxLayout(pTabCornerWidget);
pHLayout->addWidget(pLabelTime);
pHLayout->addWidget(pButton);
mUI.tabWidget->setCornerWidget(pTabCornerWidget, Qt::TopRightCorner);
If you want do this stuff, I recommend you use QTabBar instead of QTabWidget. For example, your code can be (remember, that it's just a very simple example):
// Here some first widget
QWidget *wid1 = new QWidget(this);
QHBoxLayout *wid1Lay = new QHBoxLayout(wid1);
wid1Lay->addWidget(new QLabel(tr("Widget1")));
// Here some second widget
QWidget *wid2 = new QWidget(this);
QHBoxLayout *wid2Lay = new QHBoxLayout(wid2);
wid2Lay->addWidget(new QLabel(tr("Widget2")));
// Here some third widget
QWidget *wid3 = new QWidget(this);
QHBoxLayout *wid3Lay = new QHBoxLayout(wid3);
wid3Lay->addWidget(new QLabel(tr("Widget3")));
// Here your Tab bar with only bars
QTabBar *bar = new QTabBar(this);
bar->addTab("One");
bar->addTab("Two");
bar->addTab("Three");
// Here some label (for example, current time) and button
QLabel *lab = new QLabel(tr("Some text"), this);
QPushButton *but = new QPushButton(tr("Push"), this);
// Main layouts
QVBoxLayout *vLay = new QVBoxLayout(ui->centralWidget);
QHBoxLayout *hLay = new QHBoxLayout();
vLay->addLayout(hLay);
hLay->addWidget(bar);
// Spacer for expanding left and right sides
hLay->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
hLay->addWidget(lab);
hLay->addWidget(but);
vLay->addWidget(wid1);
vLay->addWidget(wid2);
vLay->addWidget(wid3);
// Some simple connect with lambda for navigation
connect(bar, &QTabBar::currentChanged, [=] (int index) {
wid1->setVisible(false);
wid2->setVisible(false);
wid3->setVisible(false);
switch(index) {
case 0: wid1->setVisible(true);
break;
case 1: wid2->setVisible(true);
break;
case 2: wid3->setVisible(true);
break;
default:{}
}
});
emit bar->currentChanged(0);

Multiple widgets on a QDockWidget

I'm making a little app for applying various filters on an image using Qt and C++.
My question is, is it possible to add multiple widgets on a QDockWidget ?
As i want to add buttons for re-applying the last 5 filters on the dock.
Here is an example of what i want to achieve.
It is possible add to multiple QWidgets into any QWidget.
It looks like you probably want to do something like this:
QDockWidget dock(QLatin1String("Last filters"));
QWidget* multiWidget = new QWidget();
QVBoxLayout* layout = new QVBoxLayout();
QPushButton* filter1 = new QPushButton(QLatin1String("Filter number 1"));
QPushButton* filter2 = new QPushButton(QLatin1String("Filter number 2"));
QPushButton* filter3 = new QPushButton(QLatin1String("Filter number 3"));
QPushButton* filter4 = new QPushButton(QLatin1String("Filter number 4"));
QPushButton* filter5 = new QPushButton(QLatin1String("Filter number 5"));
QLabel* label = new QLabel(QLatin1String("QPushButtons"));
layout->addWidget(filter1);
layout->addWidget(filter2);
layout->addWidget(filter3);
layout->addWidget(filter4);
layout->addWidget(filter5);
layout->addWidget(label);
multiWidget->setLayout(layout);
dock.setWidget(multiWidget);

Layouts doesn't work in Qt

i'm having a problem with layouts in Qt. I'm trying to compile this code, with 2 horizontal layouts and a vertical main layout. Each horizontal layout have 3 buttons and both horizontal layout are incorporated in the vertical layout. But after i compile this code, i can only see a small window with just the "Exit" button.
firstline = new QHBoxLayout(this);
secondline = new QHBoxLayout(this);
layout = new QVBoxLayout(this);
eat = new QPushButton("Eat", this);
drink = new QPushButton("Drink", this);
smoke = new QPushButton("Smoke", this);
save = new QPushButton("Save", this);
load = new QPushButton("Load", this);
exit = new QPushButton("Exit", this);
firstline->addWidget(eat);
firstline->addWidget(drink);
firstline->addWidget(smoke);
secondline->addWidget(save);
secondline->addWidget(load);
secondline->addWidget(exit);
layout->addLayout(firstline);
layout->addLayout(secondline);
setLayout(layout);
You are already setting the layout for your dialog through these statements...
firstline = new QHBoxLayout(this);
secondline = new QHBoxLayout(this);
So call their constructors without specifying their parent widget.
firstline = new QHBoxLayout();
secondline = new QHBoxLayout();
This would display your layout as you are expecting.