Need help when autohiding a tabBar in Qt with C++ - c++

I am new to Qt programming in C++. I am using Visual Studio 2019 as editor. I made two buttons (b1 and b2) and a tab widgets. They are visible properly in the UI as shown in the following Fig.
What I want? I want to auto hide the tab area.
What I did? This is the code that contains the widgets (tab and buttons).
main.cpp
#include "tabbar.h"
#include <QtWidgets/QApplication>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
TabBar w;
w.show();
return a.exec();
}
tabbar.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_tabbar.h"
#include<qtabbar.h>
#include<qpushbutton.h>
#include<qgridlayout.h>
#include<qdebug.h>
class TabBar : public QMainWindow
{
Q_OBJECT
public:
TabBar();
};
tabbar.cpp
#include "tabbar.h"
TabBar::TabBar()
{
QPushButton* b1 = new QPushButton("b1");
QPushButton* b2 = new QPushButton("b2");
QTabBar* tabBar = new QTabBar;
tabBar->addTab("Tab 1");
tabBar->addTab("Tab 2");
tabBar->setTabText(0, "Hello tab 1");
tabBar->setTabText(1, "Hello tab 2");
QString str;
str = tabBar->tabText(1);
qDebug() << str;
tabBar->setTabToolTip(0, "ToolTip for tab 1");
tabBar->setTabToolTip(1, "ToolTip for tab 2");
tabBar->autoHide();
QGridLayout* layout = new QGridLayout;
layout->addWidget(b1, 0, 0);
layout->addWidget(b2, 0, 1);
layout->addWidget(tabBar, 0, 2);
QWidget* wid = new QWidget;
wid->setLayout(layout);
setCentralWidget(wid);
}
The main problem: Even though I call the function autoHide() through tabBar() function the tab are is not hidden. Did I miss something?

the method you are using is actually the "getter" of the property, you need to call the setter setAutoHide(bool hide), furthermore you should read the doc:
autoHide : bool
If true, the tab bar is automatically hidden when it contains less than 2 tabs.
so with 2 tabs nothing is going to happen

Related

How to programmatically change style sheet of buttons in Qt?

I have so many buttons on a dialog and I want to change style sheets of them under some conditions.
Button object names are like below:
btn_1
btn_2
btn_3
..
btn_20
When I clicked one of these numerical buttons and later to another simple button, I want to change first clicked numerical button style sheet. How can I access that selected numerical button?
Edit:
What I mean by picture
I am trying to set colors of left column buttons (has numerically ordered object names) with right column buttons. User will be clicked numerical buttons first and then color named buttons.
You have to use the setStyleSheet method but you have to keep the reference of the button pressed, and that can be done using the sender method that returns the object that emitted the signal.
#include <QtWidgets>
class MainWindow: public QMainWindow{
Q_OBJECT
public:
MainWindow(QWidget *parent=nullptr):
QMainWindow(parent),
current_button(nullptr)
{
QWidget *widget = new QWidget;
setCentralWidget(widget);
QHBoxLayout *hlay = new QHBoxLayout(widget);
QVBoxLayout *number_lay = new QVBoxLayout;
QVBoxLayout *color_lay = new QVBoxLayout;
hlay->addLayout(number_lay);
hlay->addLayout(color_lay);
for(int i=0; i<20; i++){
QPushButton *button = new QPushButton(QString("btn_%1").arg(i+1));
connect(button, &QPushButton::clicked, this, &MainWindow::number_clicked);
number_lay->addWidget(button);
}
color_lay->addStretch();
for(const QString & colorname: {"Red", "Green", "Blue"}){
QPushButton *button = new QPushButton(colorname);
connect(button, &QPushButton::clicked, this, &MainWindow::color_clicked);
color_lay->addWidget(button);
button->setProperty("color", colorname.toLower());
button->setStyleSheet(QString("background-color: %1").arg(colorname));
}
color_lay->addStretch();
}
private slots:
void number_clicked(){
current_button = qobject_cast<QPushButton *>(sender());
}
void color_clicked(){
if(current_button){
QString colorname = sender()->property("color").toString();
current_button->setStyleSheet(QString("background-color: %1").arg(colorname));
}
}
private:
QPushButton *current_button;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "main.moc"
When you click on the first button, get its name using the method objectName(), then when you need to change the style, just specify in the method
setStyleSheet(QString(QPushButton#) + button->objectName() + QString("{ ... }");
I can write the example-program, but I do not fully understand what you want

QT - How to place widgets in mutually exclusive groups?

I'm looking to do something like the following:
example
But with radio buttons instead of checkboxes. In the above picture widgets are placed into groups which can be enabled/disabled, but I want only 1 group to be enabled at a time (in the example both Group A and Group C are enabled).
I did the above example using QGroupBox, but it only provides normal checkboxes, not radio buttons as far as I can tell.
The ckeckbox that appears in the QGroupBox is not a QCheckBox, it is just a drawing. So a possible solution is to create a class that manages the QGroupBox checked.
#include <QApplication>
#include <QGroupBox>
#include <QLineEdit>
#include <QRadioButton>
#include <QSlider>
#include <QVBoxLayout>
class ExclusiveManager: public QObject{
public:
using QObject::QObject;
void addGroupBox(QGroupBox *groupbox){
if(groupbox){
groupbox->blockSignals(true);
groupbox->setChecked(m_groupboxs.isEmpty());
groupbox->blockSignals(false);
m_groupboxs << groupbox;
connect(groupbox, &QGroupBox::toggled, this, &ExclusiveManager::onToggled);
}
}
private slots:
void onToggled(bool on){
QGroupBox *groupbox = qobject_cast<QGroupBox *>(sender());
if(on){
for(QGroupBox *g: m_groupboxs){
if(g != groupbox && g->isChecked()){
g->blockSignals(true);
g->setChecked(false);
g->blockSignals(false);
}
}
}
else{
groupbox->blockSignals(true);
groupbox->setChecked(false);
groupbox->blockSignals(false);
}
}
private:
QList<QGroupBox *> m_groupboxs;
};
class Widget: public QWidget{
public:
Widget(QWidget *parent=nullptr):QWidget(parent){
setLayout(new QVBoxLayout);
ExclusiveManager *manager = new ExclusiveManager(this);
group_a = new QGroupBox("Group A");
group_a->setCheckable(true);
group_b = new QGroupBox("Group B");
group_b->setCheckable(true);
group_c = new QGroupBox("Group C");
group_c->setCheckable(true);
layout()->addWidget(group_a);
layout()->addWidget(group_b);
layout()->addWidget(group_c);
manager->addGroupBox(group_a);
manager->addGroupBox(group_b);
manager->addGroupBox(group_c);
QVBoxLayout *layA = new QVBoxLayout();
layA->addWidget(new QLineEdit);
group_a->setLayout(layA);
QVBoxLayout *layB = new QVBoxLayout();
layB->addWidget(new QRadioButton("Option 1"));
layB->addWidget(new QRadioButton("Option 2"));
group_b->setLayout(layB);
QVBoxLayout *layC = new QVBoxLayout();
layC->addWidget(new QSlider(Qt::Horizontal));
group_c->setLayout(layC);
}
private:
QGroupBox *group_a;
QGroupBox *group_b;
QGroupBox *group_c;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
You can link each QGroupBox'toggled signal with the other's setDisabled slot.
ps: I know it's too late but I'm just putting it here if someone else needs it.

Display text from QLineEdit in a QTextEdit already containing some text and update it in real time

What is the procedure to make a text being written in a QLineEdit widget, dynamically display inside a QTextEdit that already contains some text?
For example, let us say that a QLineEdit asks for a name where one writes "John". Is it possible to display it in real time inside a QTextEdit containing :
The name is + textFromQLineEdit + , age 24 ?
The displayed text has to dynamically take into account the changes being made to the QLineEdit so that the user does not need to press a button or press enter to see his/her name appear.
The following is the minimal code for connecting the two widgets to each other using the signal textChanged() from QLineEdit and the slot setText() from QTextEdit (which does not allow for adding some text before and after the text from the QLineEdit) :
#include <QLineEdit>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QTextEdit>
#include <QApplication>
class SmallWindow : public QWidget
{
Q_OBJECT
public:
SmallWindow();
private:
QLineEdit *nameLine;
QTextEdit *textBox;
};
SmallWindow::SmallWindow() : QWidget()
{
setFixedSize(300,250);
QLineEdit *nameLine = new QLineEdit;
QTextEdit *textBox = new QTextEdit;
QWidget::connect(nameLine,SIGNAL(textChanged(QString)),textBox,SLOT(setText(QString)));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(nameLine);
layout->addWidget(textBox);
QGroupBox *group = new QGroupBox(this);
group->setLayout(layout);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
SmallWindow window;
window.show();
app.exec();
}
#include "main.moc"
What should be done to keep the text before and after the QLineEdit text in place and updating the QTextEdit box in real time?
Create special slot:
void SmallWindow::pasteText(const QString& str)
{
textBox->setText(QString("The name is %1 , age 24").arg(str));
}
and don't use textChanged() signal because you need only one name accepted by user, so you need QLineEdit::editingFinished() (or maybe QLineEdit::returnPressed(), it depends on your needs)
connect(nameLine,SIGNAL(editingFinished(QString)),this,SLOT(pasteText(QString)));
Also you don't need QWidget::connect, because you write this code inside QObject subclass, so it is not necessary.
Also these lines:
QLineEdit *nameLine = new QLineEdit;
QTextEdit *textBox = new QTextEdit;
should be:
nameLine = new QLineEdit;
textBox = new QTextEdit;
Create an own slot for the text update. I think that you have also some errors in your code.
Widgets nameLine and textBox are already defined in the SmallWindow.h. You probably want to create them in SmallWindow.cpp following way:
nameLine = new QLineEdit;
textBox = new QTextEdit;
Also GroupBox group is not set to any layouts. Perhaps you want to create one layout more and set the widget there?
QVBoxLayout *mainlayout = new QVBoxLayout;
mainlayout->addWidget(group);
this->setLayout(mainlayout);
If you create an own slot for the text update, you can just change text content of the textBox there:
SmallWindow.h
#ifndef SMALLWINDOW_H
#define SMALLWINDOW_H
#include <QLineEdit>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QTextEdit>
class SmallWindow : public QWidget
{
Q_OBJECT
public:
SmallWindow();
private slots:
void updateLineEditText(QString name);
private:
QLineEdit *nameLine;
QTextEdit *textBox;
};
#endif // SMALLWINDOW_H
SmallWindow.cpp
#include "SmallWindow.h"
SmallWindow::SmallWindow() : QWidget()
{
setFixedSize(300,250);
nameLine = new QLineEdit;
textBox = new QTextEdit;
connect(nameLine,SIGNAL(textChanged(QString)),this,
SLOT(updateLineEditText(QString)));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(nameLine);
layout->addWidget(textBox);
QGroupBox *group = new QGroupBox(this);
group->setLayout(layout);
QVBoxLayout *mainlayout = new QVBoxLayout;
mainlayout->addWidget(group);
this->setLayout(mainlayout);
}
void SmallWindow::updateLineEditText(QString name) {
QString textEditString("The name is ");
textEditString.append(name);
textEditString.append(", age 24 ?");
textBox->setText(textEditString);
}
This is a minimal example in Qt 5, using C++11. It is about as concise as it would be in Python. If you are using Qt 5, then your question should have looked exactly as it does below, save for the connect statement. This is what "minimal" means when it comes to Qt. Avoid the fluff and boilerplate that doesn't add to the problem. There's no need to have a separate class for the window in such a simple example.
#include <QVBoxLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
QVBoxLayout layout(&window);
QLineEdit name;
QTextEdit text;
layout.addWidget(&name);
layout.addWidget(&text);
QObject::connect(&name, &QLineEdit::textChanged, [&](const QString & name){
text.setPlainText(QString("The name is %1, age 24.").arg(name));
});
window.show();
return app.exec();
}
The same in Qt 4 is below - note the absence of any manual memory management. That's how your question ideally should have looked for Qt 4, without the slot's implementation of course. You can use the connectSlotsByName or an explicit connect, of course - that's just a matter of style.
#include <QVBoxLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QApplication>
class Window : public QWidget {
Q_OBJECT
QVBoxLayout m_layout; // not a pointer!
QLineEdit m_name; // not a pointer, must come after the layout!
QTextEdit m_text;
Q_SLOT void on_name_textChanged(const QString & name) {
m_text.setPlainText(QString("The name is %1, age 24.").arg(name));
}
public:
Window() : m_layout(this) {
m_layout.addWidget(&m_name);
m_layout.addWidget(&m_text);
m_name.setObjectName("name");
QMetaObject::connectSlotsByName(this);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Window window;
window.show();
return app.exec();
}
#include "main.moc"

Creating and laying out widgets using a for loop in Qt

So i wanted to create 5 buttons in Qt but instead, create just one button and put it in a for loop so i don't have to create each of the 5 buttons manually. I tried different ways but all proved futile. I'm new to C++ and Qt.
Here are the codes;
show.h
#ifndef SHOW_H
#define SHOW_H
#include <QDialog>
#include <QPushButton>
#include <QVBoxLayout>
class Show : public QDialog {
Q_OBJECT
public:
explicit Show(QWidget *parent = 0);
~Show();
private:
QPushButton *button;
};
#endif // SHOW_H
show.cpp
#include "show.h"
#include "ui_show.h"
Show::Show(QWidget *parent) : QDialog(parent) {
int a = 5;
button = new QPushButton[a];
button->setText("Ok");
QVBoxLayout *layout = new QVBoxLayout[a];
for (int i = 0; i < sizeof(button)/4; i++) {
/*here, i wanted to do something like this;
'layout[i].addWidget(button[i]);' but didn't work*/
layout[i].addWidget(button);
}
setLayout(layout);
}
Show::~Show() {
}
main.cpp
#include "show.h"
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Show *dialog = new Show;
dialog->show();
return a.exec();
}
After i run the code, i only see one button.
Your help is deeply appreciated. Thank you!!
QPushButton* pButton = new QPushButton("Ok");
This creates a single instance of a QPushButton.
You can add the button to a layout with a call to Layout::addWidget, which internally calls addItem.
As the documentation states for addItem: -
Note: The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it
So your current code creates a single button and as it gets added to each successive layout, it is removed from the layout in which it was previously added.
You're creating one button and adding the same button 5 times. If you want 5 buttons in 5 layouts using a loop, then you need 5 separate instances of the button: -
for (int i=0; i<5; ++i)
{
QPushButton* pButton = new QPushButton("Ok");
layout[i].addWidget(pButton);
}
In your show.cpp change the lines
Show::Show(QWidget *parent) : QDialog(parent) {
int a = 5;
// You only need one layout for all buttons, not one per button.
QVBoxLayout *layout = new QVBoxLayout( this );
for (int i = 0; i < a; i++) {
QPushButton * newButton = new QPushButton( this );
newButton ->setText( "Ok" );
layout->addWidget( newButton );
}
setLayout(layout);
}
Show::~Show() {
}
The layout expects a pointer and you only need one layout instead one per button.

Qt custom widget not showing child widgets

I have a custom widget with some standard child widgets inside. If I make a separate test project and redefine my custom widget to inherit QMainWindow, everything is fine. However, if my custom widget inherits QWidget, the window opens, but there are no child widgets inside.
This is the code:
controls.h:
#include <QtGui>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
class Controls : public QWidget
{
Q_OBJECT
public:
Controls();
private slots:
void render();
private:
QWidget *frame;
QWidget *renderFrame;
QVBoxLayout *layout;
QLineEdit *rayleigh;
QLineEdit *mie;
QLineEdit *angle;
QPushButton *renderButton;
};
controls.cpp:
#include "controls.h"
Controls::Controls()
{
frame = new QWidget;
layout = new QVBoxLayout(frame);
rayleigh = new QLineEdit;
mie = new QLineEdit;
angle = new QLineEdit;
renderButton = new QPushButton(tr("Render"));
layout->addWidget(rayleigh);
layout->addWidget(mie);
layout->addWidget(angle);
layout->addWidget(renderButton);
frame->setLayout(layout);
setFixedSize(200, 400);
connect(renderButton, SIGNAL(clicked()), this, SLOT(render()));
}
main.cpp:
#include <QApplication>
#include "controls.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Controls *controls = new Controls();
controls->show();
return app.exec();
}
This opens up a window with correct dimensions, but with no content inside.
Bear in mind this is my first day using Qt. I need to make this work without inheriting QMainWindow because later on I need to put this on a QMainWindow.
You're missing a top level layout:
Controls::Controls()
{
... (yoour code)
QVBoxLayout* topLevel = new QVBoxLayout(this);
topLevel->addWidget( frame );
}
Or, if frame is not used anywhere else, directly:
Controls::Controls()
{
layout = new QVBoxLayout(this);
rayleigh = new QLineEdit;
mie = new QLineEdit;
angle = new QLineEdit;
renderButton = new QPushButton(tr("Render"));
layout->addWidget(rayleigh);
layout->addWidget(mie);
layout->addWidget(angle);
layout->addWidget(renderButton);
setFixedSize(200, 400);
connect(renderButton, SIGNAL(clicked()), this, SLOT(render()));
}
Note that setLayout is done automatically when QLayout is created (using parent widget)
You'll want to set a layout on your Controls class for managing its child sizes. I'd recommend removing your frame widget.
controls.cpp
Controls::Controls()
{
layout = new QVBoxLayout(this);
.
.
.
}
main.cpp
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}