I am writing a small utility composed by :
1) 4 QToolBar
2) 1 QToolBox
3) 1 QMenu
4) 1 QGraphicsView
I have some buttons on the QToolBox, each button represents a grid with different spacing.
Everytime a user clicks a button on the QToolBox the spacing on the QGraphicsScene` changes.
I use daily the new notation and never had any problems with it until I had to use an "abstract" entity such as the QAbstractButton approach.
the problem I have is that I don't seem to properly set the connection signal/slot of the QAbstractButton:
mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void backgroundButtonClicked(QAbstractButton *button);
private:
void createToolBox();
QButtonGroup *buttonGroup;
QButtonGroup *backgroundButtonGroup;
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
createToolBox();
scene = new DiagramScene(itemMenu,this);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(toolBox);
view = new QGraphicsView(scene);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
}
void MainWindow::backgroundButtonClicked(QAbstractButton *button)
{
QList<QAbstractButton*> buttons = backgroundButtonGroup->buttons();
foreach (QAbstractButton *myButton, buttons) {
if(myButton != button)
button->setChecked(false);
}
QString text = button->text();
if(text == tr("Blue Grid"))
scene->setBackgroundBrush(QPixmap(":/images/background1.png"));
else if(text == tr("White Grid"))
scene->setBackgroundBrush(QPixmap(":/images/background2.png"));
else if(text == tr("Gray Grid"))
scene->setBackgroundBrush(QPixmap(":/images/background3.png"));
else
scene->setBackgroundBrush(QPixmap(":/images/background4.png"));
scene->update();
view->update();
}
void MainWindow::createToolBox()
{
buttonGroup = new QButtonGroup(this);
buttonGroup->setExclusive(false);
QGridLayout *layout = new QGridLayout;
layout->addWidget(createCellWidget(tr("Conditional"), DiagramItem::Conditional), 0, 0);
layout->addWidget(createCellWidget(tr("Process"), DiagramItem::Step), 0, 1);
layout->addWidget(createCellWidget(tr("Input/Output"), DiagramItem::Io), 1, 0);
QToolButton *textButton = new QToolButton;
textButton->setCheckable(true);
buttonGroup->addButton(textButton, InsertTextButton);
textButton->setIcon(QIcon(QPixmap(":/images/textpointer.png")));
textButton->setIconSize(QSize(50, 50));
QWidget *itemWidget = new QWidget;
itemWidget->setLayout(layout);
backgroundButtonGroup = new QButtonGroup(this);
// This is my first attempt but it does not work:
QObject::connect(backgroundButtonGroup, &QAbstractButton::clicked, this, &QAbstractButton::backgroundButtonClicked());
}
Different options I tried are:
1) via static_cast<> trying to cast it into the QAbstractButton as shown below:
QObject::connect(static_cast<QAbstractButton*>(backgroundButtonGroup), this, static_cast<QAbstractButton>(backgroundButtonClicked(void)));
2) Also I tried the following according to official documentation but that also didn't work:
QObject::connect(backgroundButtonGroup, &QAbstractButton::clicked, this, &QAbstractButton::backgroundButtonClicked);
Adding to that I was digressing towards the following structure below (heading to Q_INVOKE) but I am afraid I was over complicating the issue, but just wanted to include the additional solution I was trying to explore:
const bool connected = connect(sender, &Sender::aSignal,
receiver, &Receiver::aSlot);
Q_ASSERT(connected);
Q_UNUSED(connected);
Thanks for pointing to the right direction for solving this issue.
You have the following errors:
QButtonGroup does not have the clicked signal but buttonClicked.
You must not use ().
The syntax in general is: obj, & Class_of_obj, in your case backgroundButtonGroup is QButtonGroup and this is MainWindow so preliminarily the syntax is:
QObject::connect(backgroundButtonGroup, &QButtonGroup::buttonClicked, this, &MainWindow::backgroundButtonClicked);
But buttonClicked is overload then you must use QOverload to indicate the signature.
Considering the above, the solution is:
QObject::connect(backgroundButtonGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked), this, &MainWindow::backgroundButtonClicked);
I recommend you check:
the QButtonGroup docs where it shows the example of how to make the connection.
In addition to New Signal Slot Syntax that indicates the various cases
Related
I am new to Qt application programming, I have a task to complete i.e
I need to create a paint like application
Based on left side panel objects Right side List must display..
PFA
so now i am stuck with displaying right side panel
To be more clear...
I used left side QGroupBox with Push Buttons.
Right side i used QGridLayout to show specific objects.
Center graphics view ( ignore this point).
now when I am calling the function after push button clicked Button 1 : should display only numbered objects Button 2 : should display only other objects Button 3 : should display empty plane Button N : should display all objects
could some one suggest me with small code for it.... like
ui->gridLayout_1->... ( to hide previous options )
when called second time ui->gridLayout_1->addWidget(label0,0,0); should display relevant
when called for third time display all objects ui->gridLayout_1->addWidget(label0,0,0);
Edit: After seeing a better example of what you were trying to accomplish I threw this together using QStackedWidget. I think the below code mimics the behavior you are looking for. This has replaced my old answer as I didn't want to add any unnecessary confusion.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QMainWindow>
#include <QPushButton>
#include <QStackedWidget>
#include <QWidget>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
QWidget *central_widget = new QWidget(this);
QHBoxLayout *central_layout = new QHBoxLayout();
QVBoxLayout *ctl_btn_layout = new QVBoxLayout();
QPushButton *ctl_btn_1 = new QPushButton("1", central_widget);
QPushButton *ctl_btn_2 = new QPushButton("2", central_widget);
QStackedWidget *widget_stack = new QStackedWidget(central_widget);
QWidget *page_0 = new QWidget();
QWidget *page_1 = new QWidget();
QGridLayout *page_1_layout = new QGridLayout();
QPushButton *r_btn = new QPushButton("R", page_1);
QPushButton *j_btn = new QPushButton("J", page_1);
QPushButton *l_btn = new QPushButton("L", page_1);
QPushButton *o_btn = new QPushButton("O", page_1);
QPushButton *v_btn = new QPushButton("V", page_1);
QPushButton *f_btn = new QPushButton("F", page_1);
QWidget *page_2 = new QWidget();
QVBoxLayout *page_2_layout = new QVBoxLayout();
QPushButton *btn_1 = new QPushButton("1", page_2);
QPushButton *btn_2 = new QPushButton("2", page_2);
QPushButton *btn_3 = new QPushButton("3", page_2);
QPushButton *btn_4 = new QPushButton("4", page_2);
QPushButton *btn_5 = new QPushButton("5", page_2);
QPushButton *btn_6 = new QPushButton("6", page_2);
QLabel *r = new QLabel("R", central_widget);
QLabel *j = new QLabel("J", central_widget);
QLabel *id = new QLabel("ID", central_widget);
QLabel *x = new QLabel("X", central_widget);
QLabel *y = new QLabel("Y", central_widget);
QLineEdit *r_line = new QLineEdit(central_widget);
QLineEdit *j_line = new QLineEdit(central_widget);
QLineEdit *id_line = new QLineEdit(central_widget);
QLineEdit *x_line = new QLineEdit(central_widget);
QLineEdit *y_line = new QLineEdit(central_widget);
QVBoxLayout *lines_layout = new QVBoxLayout();
public slots:
void ctl_btn_1_clicked();
void ctl_btn_2_clicked();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ctl_btn_layout->addWidget(ctl_btn_1);
connect(ctl_btn_1, SIGNAL(clicked(bool)), this, SLOT(ctl_btn_1_clicked()));
ctl_btn_layout->addWidget(ctl_btn_2);
connect(ctl_btn_2, SIGNAL(clicked(bool)), this, SLOT(ctl_btn_2_clicked()));
central_layout->addLayout(ctl_btn_layout);
page_1_layout->addWidget(r_btn, 0, 0, 1, 1);
page_1_layout->addWidget(j_btn, 1, 1, 1, 1);
page_1_layout->addWidget(l_btn, 2, 0, 1, 1);
page_1_layout->addWidget(o_btn, 3, 1, 1, 1);
page_1_layout->addWidget(v_btn, 4, 2, 1, 1);
page_1_layout->addWidget(f_btn, 5, 3, 1, 1);
page_1->setLayout(page_1_layout);
page_2_layout->addWidget(btn_1);
page_2_layout->addWidget(btn_2);
page_2_layout->addWidget(btn_3);
page_2_layout->addWidget(btn_4);
page_2_layout->addWidget(btn_5);
page_2_layout->addWidget(btn_6);
page_2->setLayout(page_2_layout);
widget_stack->addWidget(page_0); //Empty QWidget.
widget_stack->addWidget(page_1);
widget_stack->addWidget(page_2);
central_layout->addWidget(widget_stack);
lines_layout->addWidget(r);
r->hide();
lines_layout->addWidget(r_line);
r_line->hide();
lines_layout->addWidget(j);
j->hide();
lines_layout->addWidget(j_line);
j_line->hide();
lines_layout->addWidget(id);
id->hide();
lines_layout->addWidget(id_line);
id_line->hide();
lines_layout->addWidget(x);
x->hide();
lines_layout->addWidget(x_line);
x_line->hide();
lines_layout->addWidget(y);
y->hide();
lines_layout->addWidget(y_line);
y_line->hide();
central_layout->addLayout(lines_layout);
central_widget->setLayout(central_layout);
setCentralWidget(central_widget);
}
MainWindow::~MainWindow()
{
}
void MainWindow::ctl_btn_1_clicked()
{
if (j->isVisible())
{
j->hide();
j_line->hide();
}
r->show();
r_line->show();
if (!id->isVisible())
{
id->show();
id_line->show();
x->show();
x_line->show();
y->show();
y_line->show();
}
widget_stack->setCurrentIndex(1);
}
void MainWindow::ctl_btn_2_clicked()
{
if (r->isVisible())
{
r->hide();
r_line->hide();
}
j->show();
j_line->show();
if (!id->isVisible())
{
id->show();
id_line->show();
x->show();
x_line->show();
y->show();
y_line->show();
}
widget_stack->setCurrentIndex(2);
}
This works by using a QStackedWidget, which is populated with 3 QWidgets. The first widget is completely empty to mimic the empty page from your example. The second and third widgets have layouts that hold your different button configurations, and the QStackedWidget will flip between pages through functions connected to the button slots.
Note that I tend not to use the form designer that comes w/ Qt Creator, preferring pure C++ UI design. It doesn't matter how you set it up, with or without the form, I just find it easier this way. Main.cpp was left as default from a new Qt GUI project with the generate form box unchecked.
I have a groupbox that contains some pushbuttons and sliders. I want that when I click on a button, a new groupbox that is the same with the former one should appear under the first one. Whenever I click on the button, same situation should happen dynamically. Since I need up to 32 groupbox like that, I don't want to put all groupboxes manually. So, how can I do this?
First off, a layout is highly recommended.
Here is an example (I have done this before). You can derive a class from QScrollArea, then set in the constructor the layouts you want to have.
In here a simple button called Add is in the window.
If you press it, a row gets added and initialized with default values (0, 0, 0) <- integers.
In the live program, I load the values from a file/database and initialize it then.
You may want to use different layout(s) and a different setup, but this should give you the idea. I'm sure you get where you want with a little more experimenting.
//Structure to keep track of the added widgets easier
struct ItemRow
{
ItemRow(QLineEdit *entry, QLineEdit *amount, QComboBox *box)
: m_Entry(entry)
, m_Amount(amount)
, m_Box(box)
{ }
ItemRow(void)
: m_Entry(nullptr)
, m_Amount(nullptr)
, m_Box(nullptr)
{ }
QLineEdit *m_Entry;
QLineEdit *m_Amount;
QComboBox *m_Box;
};
The class declaration.
class MyScrollArea : public QScrollArea
{
Q_OBJECT
public:
explicit MyScrollArea(QWidget *parent = 0);
~MyScrollArea();
//...
void OnAddButtonPressed(void);
void DrawButtonLayout(void);
void AddRow(int val1, int val2, int val3); //Use own parameters
private:
QVBoxLayout *m_LayoutFirstRow;
QVBoxLayout *m_LayoutSecondRow;
QVBoxLayout *m_LayoutThirdRow;
//...
QVBoxLayout *m_LayoutButton;
//...
QList<QPushButton*> m_Buttons;
QVector<ItemRow> m_ItemRows;
}
The implementation.
MyScrollArea::MyScrollArea(QWidget *parent) :
QScrollArea(parent),
ui(new Ui::MyScrollArea)
{
ui->setupUi(this);
setWidget(new QWidget);
setWidgetResizable(true);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
m_LayoutFirstRow = new QVBoxLayout();
m_LayoutSecondRow = new QVBoxLayout();
m_LayoutThirdRow = new QVBoxLayout();
m_LayoutButton = new QVBoxLayout();
widget()->setLayout(mainLayout);
mainLayout->addLayout(m_LayoutFirstRow);
mainLayout->addLayout(m_LayoutSecondRow);
mainLayout->addLayout(m_LayoutThirdRow);
mainLayout->addLayout(m_LayoutButton);
DrawButtonLayout();
}
RewardDialog::~RewardDialog()
{
delete ui;
}
void MyScrollArea::OnAddButtonPressed(void)
{
AddRow(0, 0, 0);
}
void MyScrollArea::DrawButtonLayout(void)
{
QPushButton *addBtn = new QPushButton("Add");
connect(addBtn, SIGNAL(clicked()), this, SLOT(OnAddButtonPressed()));
m_LayoutButton->addWidget(addBtn);
m_Buttons.push_back(addBtn); //Keep somewhere track of the button(s) if needed - example: put in QList (not the best approach though)
}
void MyScrollArea::AddRow(int val1, int val2, int val3)
{
QLineEdit *pEntry = new QLineEdit(QString::number(val1));
pEntry->setValidator(new QIntValidator());
QLineEdit *pAmount = new QLineEdit(QString::number(val2));
pAmount->setValidator(new QIntValidator());
QComboBox *pBox = new QComboBox();
InitComboBox(pBox, val3); //Initialize the combo-box (use connect if you wish) - code not included
m_LayoutFirstRow->addWidget(pEntry);
m_LayoutSecondRow->addWidget(pAmount);
m_LayoutThirdRow->addWidget(pBox);
ItemRow row;
row.m_Entry = pEntry;
row.m_Amount = pAmount;
row.m_Box = pBox;
m_ItemRows.push_back(row);
}
Leave a comment if something seems wrong, I put this together in Notepad++.
Note: The documentation-link is for QT4.8, as 5.3 is not available anymore, but my code is from version 5.3 too.
I created a this->setWindowFlags(Qt::FramelessWindowHint); and so there is no title bar. Therefore, I am implementing my own. I wanted to know, however, before I continue whether there is a standard way to add the close, minimize, and maximize buttons in a native-os looking way (i.e. on windows it should look like the windows close buttons and the same for osx and linux).
QStyle take a lot of standard icons base on OS style. You can get this icon from current OS style and then draw it by your self.
This is a simple implementation for reference.
class TitleBar : public QWidget
{
Q_OBJECT
public:
explicit TitleBar(QWidget *parent = 0)
:QWidget(parent)
{
QStyle *style = qApp->style();
QIcon closeIcon = style->standardIcon(QStyle::SP_TitleBarCloseButton);
QIcon maxIcon = style->standardIcon(QStyle::SP_TitleBarMaxButton);
QIcon minIcon = style->standardIcon(QStyle::SP_TitleBarMinButton);
QPushButton *min = new QPushButton(this);
QPushButton *max = new QPushButton(this);
QPushButton *close = new QPushButton(this);
min->setIcon(minIcon);
max->setIcon(maxIcon);
close->setIcon(closeIcon);
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setSpacing(0);
layout->addWidget(min);
layout->addWidget(max);
layout->addWidget(close);
setLayout(layout);
}
};
Basically I have a QTabWidget. At first it has a form widget, widget1. After that form is done being interacted with, the new widget2 form should take over that same tab.
I thought tabWidget->setCurrentWidget(new widget2()); would work, but it's basically an overload for setCurrentIndex(int).
Does anyone know of a way to do this?
You can use a QStackedWidget for this type of thing, in a tab or elsewhere.
Put all the widgets you'll want to display in that tab inside a single QStackedWidget, and place that stacked widget in a tab.
Here's a quick'n'dirty demo:
#include <QtGui>
class W: public QWidget
{
Q_OBJECT
public:
W(QWidget *parent=0): QWidget(parent)
{
// stacked widget displays one of its "children" at a time
QStackedWidget *sw = new QStackedWidget;
QPushButton *b1 = new QPushButton("hello");
sw->addWidget(b1);
QPushButton *b2 = new QPushButton("world");
sw->addWidget(b2);
// tab widget and simplistic layout
QTabWidget *tw = new QTabWidget(this);
tw->addTab(sw, "tab");
QHBoxLayout *l = new QHBoxLayout;
l->addWidget(tw);
setLayout(l);
// signal mapper to demo the widget switching
QSignalMapper *m = new QSignalMapper(this);
connect(b1, SIGNAL(clicked()), m, SLOT(map()));
m->setMapping(b1, 1);
connect(b2, SIGNAL(clicked()), m, SLOT(map()));
m->setMapping(b2, 0);
connect(m, SIGNAL(mapped(int)), sw, SLOT(setCurrentIndex(int)));
}
};
I have a a QWidgetAction which holds a QWidget composed of a QLineEdit and a QPushButton. Once the user press the button the QWidgetAction call the trigger slot.
Now I have a QMenu which I activate with exec. The problem is that even though trigger is called (I've connected it to a print function as well to check) the menu won't close.
Regular QActions works well.
Any idea why?
P.S. Googling this issue I came across people with the same problem, but no solutions.
Years old question, but still I have an answer, hope it helps anybody!
I will describe my complete solution which not only hides the menu but also manages the visual representation.
QWidgetAction subclass: MyClass.h
class MyClass : public QWidgetAction {
Q_OBJECT
public:
MyClass(QObject* parent);
bool eventFilter(QObject*, QEvent*) override;
signals:
void mouseInside();
void mouseOutside();
protected:
QWidget* createWidget(QWidget* parent) override;
private:
QWidget* w;
QWidget* border;
QFormLayout *form;
QHBoxLayout *mainLayout;
}
QWidgetAction subclass MyClass.cpp
QWidget* MyClass::createWidget(QWidget* parent) {
w = new QWidget(parent);
border = new QWidget(parent);
mainLayout = new QHBoxLayout(w);
layout = new QFormLayout();
border->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Maximum));
border->setMaximumWidth(10);
border->setMinimumWidth(10);
border->setMaximumHeight(1000); //Anything will do but it needs a height
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->setSpacing(0);
w->setLayout(mainLayout);
mainLayout->addWidget(border);
mainLayout->addLayout(layout);
layout->setContentsMargins(6, 11, 11, 11);
layout->setSpacing(6);
// Insert your widgets here, I used a QFormLayout
QLineEdit *l = new QLineEdit(w);
form->addRow("Test", l);
// I added a button to accept input
QPushButton* b = new QPushButton("Send", w);
connect(b, SIGNAL(clicked()), this, SLOT(trigger()));
layout->addWidget(b);
w->installEventFilter(this); // This is to avoid non button clicks to close the menu
return w;
}
bool MyClass::eventFilter(QObject*, QEvent* ev) {
if (ev->type() == QEvent::MouseButtonPress
|| ev->type() == QEvent::MouseButtonDblClick
|| ev->type() == QEvent::MouseButtonRelease) {
return true;
} else if (ev->type() == QEvent::Enter) {
border->setStyleSheet("background-color: #90c8f6;");
emit mouseInside();
} else if (ev->type() == QEvent::Leave) {
border->setStyleSheet("");
emit mouseOutside();
}
return false;
}
Finally to insert the QWidgetAction in a menu, in your code add the following:
QMenu *m = new QMenu(this);
MyClass *item = new MyClass(m);
connect(item, &QAction::triggered, [=] { m->hide(); YOUR CODE HERE}); // Add your action here
// This is to give a visual cue to your item, while deselecting the stuck
// action which was previously selected
connect(item, &MyClass::mouseInside, [=] { m->setActiveAction(nullptr); });
ui->yourButton->setMenu(m);
m->addAction(item);