Qt 4: How to insert QAction in menu of mainwindow - c++

My problem is to get the QAction of custom widget from the outside and insert in menu of mainwindow. There is QActionWidget but it inserts custom widget in menu.
class QDESIGNER_WIDGET_EXPORT Photo_list: public QWidget
{
Q_OBJECT
public:
Photo_list(QWidget* parent = 0);
~Photo_list();
int count();
QString returnImagePath(const int i);
void sendSignalImageLoaded();
public slots:
void addImagePath();
void deleteImagePath();
signals:
void imageLoaded(int count);
//private:
public:
Ui::Photo_list *m_photo_list_ui;
QStringList m_image_path_list;
};
class Ui_Photo_list
{
public:
QAction *addImageAction;
QAction *deleteImageAction;
QGridLayout *gridLayout;
QListWidget *listWidget;
void setupUi(QWidget *Photo_list)
{
if (Photo_list->objectName().isEmpty())
Photo_list->setObjectName(QString::fromUtf8("Photo_list"));
Photo_list->resize(274, 210);
addImageAction = new QAction(Photo_list);
addImageAction->setObjectName(QString::fromUtf8("addImageAction"));
deleteImageAction = new QAction(Photo_list);
deleteImageAction->setObjectName(QString::fromUtf8("deleteImageAction"));
gridLayout = new QGridLayout(Photo_list);
gridLayout->setContentsMargins(0, 0, 0, 0);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
gridLayout->setHorizontalSpacing(0);
listWidget = new QListWidget(Photo_list);
listWidget->setObjectName(QString::fromUtf8("listWidget"));
listWidget->setTextElideMode(Qt::ElideMiddle);
listWidget->setMovement(QListView::Static);
listWidget->setResizeMode(QListView::Adjust);
listWidget->setViewMode(QListView::IconMode);
gridLayout->addWidget(listWidget, 0, 0, 1, 1);
retranslateUi(Photo_list);
QMetaObject::connectSlotsByName(Photo_list);
} ...
How to add QAction *addImageAction in ( * )? (see comment below)
class Ui_Cpdfa
{
public:
QAction *createPdfAction;
QWidget *centralwidget;
QGridLayout *gridLayout_2;
QGridLayout *gridLayout;
QLineEdit *pdfPathLineEdit;
QPushButton *findPdfPathButton;
Photo_list *photo_list;
QToolBar *toolBar;
QMenuBar *menuBar;
QMenu *menu;
QMenu *menu_2;
void setupUi(QMainWindow *Cpdfa)
{
if (Cpdfa->objectName().isEmpty())
Cpdfa->setObjectName(QString::fromUtf8("Cpdfa"));
Cpdfa->resize(386, 303);
createPdfAction = new QAction(Cpdfa);
createPdfAction->setObjectName(QString::fromUtf8("createPdfAction"));
centralwidget = new QWidget(Cpdfa);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
gridLayout_2 = new QGridLayout(centralwidget);
gridLayout_2->setObjectName(QString::fromUtf8("gridLayout_2"));
gridLayout = new QGridLayout();
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
pdfPathLineEdit = new QLineEdit(centralwidget);
pdfPathLineEdit->setObjectName(QString::fromUtf8("pdfPathLineEdit"));
gridLayout->addWidget(pdfPathLineEdit, 0, 0, 1, 1);
findPdfPathButton = new QPushButton(centralwidget);
findPdfPathButton->setObjectName(QString::fromUtf8("findPdfPathButton"));
gridLayout->addWidget(findPdfPathButton, 0, 1, 1, 1);
photo_list = new Photo_list(centralwidget);
photo_list->setObjectName(QString::fromUtf8("photo_list"));
gridLayout->addWidget(photo_list, 1, 0, 1, 2);
gridLayout_2->addLayout(gridLayout, 0, 0, 1, 1);
Cpdfa->setCentralWidget(centralwidget);
toolBar = new QToolBar(Cpdfa);
toolBar->setObjectName(QString::fromUtf8("toolBar"));
Cpdfa->addToolBar(Qt::TopToolBarArea, toolBar);
menuBar = new QMenuBar(Cpdfa);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
menuBar->setGeometry(QRect(0, 0, 386, 21));
menu = new QMenu(menuBar);
menu->setObjectName(QString::fromUtf8("menu"));
menu_2 = new QMenu(menuBar);
menu_2->setObjectName(QString::fromUtf8("menu_2"));
Cpdfa->setMenuBar(menuBar);
toolBar->addAction(createPdfAction); //!!(*)
toolBar->addSeparator();
menuBar->addAction(menu->menuAction());
menuBar->addAction(menu_2->menuAction());
menu->addSeparator();
menu_2->addAction(createPdfAction); //!!(*)
retranslateUi(Cpdfa);
QMetaObject::connectSlotsByName(Cpdfa);
} // setupUi

I guess you are using Qt creator and designing forms separately with the form editor.
Your problem is acutally that you want the class A(a "larger" widget which comprises the subobjects) to access members in the namespace of its subobject B(your photo_list widget).
To do this,
Move the inclusion "ui_B.h" from B.cpp to B.h
Inside B.h make the private memeber Ui::B *ui; public (or you can have A and B become friends)
After including "B.h" and having a subobject pointer *B, you can call B->ui->actionXXX in A to access all memebers of B, since B->ui points to the namespace that includes all memebers created from B.ui file (which turn out to be inside "ui_B.h")

Related

QStackedWidget and subclassed widgets showing when they shouldn't

I'm trying to use a QStackedWidget with a custom QWidget subclass. I'm working on a simple kiosk for my house that shows one of a set of widgets when the screen is tapped. This works very well IFF I create all the stacked widgets in the class that contains the QStackedWidget. When I try to add a subclassed QWidget (lots of labels, need a container), then it displays correctly, but whenever setCurrentIndex() == 0, the subclassed QWidget's showEvent() is called. This means that the first showing has two widgets visible (see image below). Because no hideEvent is ever called for my subclassed widget, it remains visible forever. Below though, you can see that hideEvent is called when the index moves away from this widget, though it immediately gets a showEvent again.
I can hide the subclassed QWidget, but it's complicated if I can't tell that the showEvent I'm getting is the one for me, so skipping it is hard.
I see a showEvent in my subclass whenever QStackedWidget's index == 0.
I have included a short example. This example is a subset but will show the issue, as I stripped out some of the extra functions that don't impact the example (MQTT, setters/getters). I'm running KUbuntu 20.04, Qt5.12.1. I have not found a bug report related to this behavior, but I am hoping I just missed it.
Edited to udpate code examples and debug output
HomeInfo.h
#ifndef HOMEINFO_H
#define HOMEINFO_H
#include <QtCore/QtCore>
#include <QtGui/QtGui>
#include <QtWidgets/QtWidgets>
#include <QtQmqtt/QtQmqtt>
#include "resizelabel.h"
#include "trafficlightwidget.h"
#include "weatherwidget.h"
class HomeInfo : public QMainWindow
{
Q_OBJECT
public:
explicit HomeInfo(QWidget *parent = nullptr);
~HomeInfo() override;
protected:
bool event(QEvent *e) override;
private:
QLabel *m_wallClockLabel;
QLabel *m_dateLabel;
QLabel *m_officeHumidityLabel;
QLabel *m_officeTempLabel;
QLabel *m_atmosphericPressureLabel;
QLabel *m_basementHumidityLabel;
QLabel *m_basementHumidityLabelName;
QLabel *m_basementTemperatureLabel;
QLabel *m_basementTemperatureLabelName;
QWidget *m_trafficLight;
QStackedWidget *m_stack;
QWidget *m_clockGridWidget;
QGridLayout *m_clockWidgetLayout;
QWidget *m_houseGridWidget;
QGridLayout *m_houseGridLayout;
WeatherWidget *m_weatherWidget;
};
#endif // HOMEINFO_H
main application HomeInfo.cpp
#include "homeinfo.h"
HomeInfo::HomeInfo(QWidget *parent) : QMainWindow(parent)
{
QPalette pal(QColor(0,0,0));
setBackgroundRole(QPalette::Window);
pal.setColor(QPalette::Window, Qt::black);
setAutoFillBackground(true);
setPalette(pal);
QFont c("Roboto-Regular", 84);
c.setBold(true);
QFont d("Roboto-Regular", 32);
QFont t("Roboto-Regular", 24);
QFont n("Roboto-Regular", 18);
m_stack = new QStackedWidget(this);
m_clockGridWidget = new QWidget();
m_clockWidgetLayout = new QGridLayout(m_clockGridWidget);
m_wallClockLabel = new QLabel();
m_wallClockLabel->setScaledContents(true);
m_wallClockLabel->setFont(c);
m_wallClockLabel->setAlignment(Qt::AlignCenter);
m_dateLabel = new QLabel();
m_dateLabel->setScaledContents(true);
m_dateLabel->setFont(d);
m_dateLabel->setAlignment(Qt::AlignCenter);
m_officeHumidityLabel = new QLabel();
m_officeHumidityLabel->setScaledContents(true);
m_officeHumidityLabel->setFont(t);
m_officeHumidityLabel->setAlignment(Qt::AlignCenter);
m_officeTempLabel = new QLabel();
m_officeTempLabel->setScaledContents(true);
m_officeTempLabel->setFont(t);
m_officeTempLabel->setAlignment(Qt::AlignCenter);
m_trafficLight = new QWidget();
m_clockWidgetLayout->setHorizontalSpacing(20);
m_clockWidgetLayout->addWidget(m_wallClockLabel, 0, 0, 3, 6);
m_clockWidgetLayout->addWidget(m_officeTempLabel, 3, 0, 1, 3);
m_clockWidgetLayout->addWidget(m_officeHumidityLabel, 3, 3, 1, 3);
m_clockWidgetLayout->addWidget(m_trafficLight, 3, 5, 3, 1);
m_clockWidgetLayout->addWidget(m_dateLabel, 4, 0, 2, 5);
m_basementHumidityLabel = new QLabel();
m_basementHumidityLabel->setScaledContents(true);
m_basementHumidityLabel->setAlignment(Qt::AlignCenter);
m_basementHumidityLabel->setFont(t);
m_basementTemperatureLabel = new QLabel();
m_basementTemperatureLabel->setScaledContents(true);
m_basementTemperatureLabel->setAlignment(Qt::AlignCenter);
m_basementTemperatureLabel->setFont(t);
m_basementTemperatureLabelName = new QLabel("Basement Temp");
m_basementTemperatureLabelName->setFont(n);
m_basementHumidityLabelName = new QLabel("Basement Humidity");
m_basementHumidityLabelName->setFont(n);
m_houseGridWidget = new QWidget();
m_houseGridLayout = new QGridLayout(m_houseGridWidget);
m_houseGridLayout->addWidget(m_basementTemperatureLabelName, 0, 0);
m_houseGridLayout->addWidget(m_basementHumidityLabelName, 0, 1);
m_houseGridLayout->addWidget(m_basementTemperatureLabel, 1, 0);
m_houseGridLayout->addWidget(m_basementHumidityLabel, 1, 1);
connect(m_stack, &QStackedWidget::currentChanged, this, &HomeInfo::stackWidgetChanged);
m_weatherWidget = new WeatherWidget();
m_weatherWidget->setVisible(false);
m_stack->addWidget(m_clockGridWidget);
m_stack->addWidget(m_houseGridWidget);
m_stack->addWidget(m_weatherWidget);
setCentralWidget(m_stack);
}
HomeInfo::~HomeInfo() = default;
void HomeInfo::stackWidgetChanged(int index)
{
qDebug() << __PRETTY_FUNCTION__;
Q_UNUSED(index)
}
bool HomeInfo::event(QEvent *e)
{
if (e->type() == QEvent::MouseButtonRelease) {
qDebug() << __PRETTY_FUNCTION__;
if (m_stack->currentIndex() == (m_stack->count() - 1)) {
m_stack->setCurrentIndex(0);
}
else {
m_stack->setCurrentIndex(m_stack->currentIndex() + 1);
}
return true;
}
return QMainWindow::event(e);
}
WeatherWidget.h
#ifndef WEATHERWIDGET_H
#define WEATHERWIDGET_H
#include <QtCore/QtCore>
#include <QtWidgets/QtWidgets>
class WeatherWidget : public QWidget
{
Q_OBJECT
public:
WeatherWidget(QWidget *parent = nullptr);
~WeatherWidget() override;
protected:
void hideEvent(QHideEvent *event) override;
void showEvent(QShowEvent *event) override;
private:
QLabel *m_outdoorTemperatureLabel;
QLabel *m_outdoorTemperatureTextLabel;
QLabel *m_outdoorHumidityLabel;
QLabel *m_outdoorHumidityTextLabel;
QLabel *m_luxLabel;
QLabel *m_luxTextLabel;
QLabel *m_uvRawLabel;
QLabel *m_uvRawTextLabel;
QLabel *m_uvIndexLabel;
QLabel *m_uvIndexTextLabel;
QLabel *m_rainTodayLabel;
QLabel *m_rainTodayTextLabel;
QLabel *m_totalRainLabel;
QLabel *m_totalRainTextLabel;
QLabel *m_airPressureMercuryLabel;
QLabel *m_airPressureMercuryTextLabel;
QLabel *m_airPressureTrendLabel;
QLabel *m_airPressureTrendTextLabel;
QGridLayout *m_layout;
};
#endif // WEATHERWIDGET_H
WeatherWidget.cpp
#include "weatherwidget.h"
WeatherWidget::WeatherWidget(QWidget *parent) : QWidget(parent)
{
QDate now = QDate::currentDate();
QFont text("Roboto-Sans", 14);
QFont content("Roboto-Sans", 20);
m_outdoorTemperatureLabel = new QLabel();
m_outdoorTemperatureLabel->setFont(content);
m_outdoorTemperatureLabel->setAlignment(Qt::AlignCenter);
m_outdoorHumidityLabel = new QLabel();
m_outdoorHumidityLabel->setFont(content);
m_outdoorHumidityLabel->setAlignment(Qt::AlignCenter);
m_luxLabel = new QLabel();
m_luxLabel->setFont(content);
m_luxLabel->setAlignment(Qt::AlignCenter);
m_uvIndexLabel = new QLabel();
m_uvIndexLabel->setFont(content);
m_uvIndexLabel->setAlignment(Qt::AlignCenter);
m_rainTodayLabel = new QLabel();
m_rainTodayLabel->setFont(content);
m_rainTodayLabel->setAlignment(Qt::AlignCenter);
m_totalRainLabel = new QLabel();
m_totalRainLabel->setFont(content);
m_totalRainLabel->setAlignment(Qt::AlignCenter);
m_airPressureMercuryLabel = new QLabel();
m_airPressureMercuryLabel->setFont(content);
m_airPressureMercuryLabel->setAlignment(Qt::AlignCenter);
m_airPressureTrendLabel = new QLabel();
m_airPressureTrendLabel->setFont(content);
m_airPressureTrendLabel->setAlignment(Qt::AlignCenter);
m_outdoorTemperatureTextLabel = new QLabel("Temperature");
m_outdoorTemperatureTextLabel->setFont(text);
m_outdoorTemperatureTextLabel->setAlignment(Qt::AlignCenter);
m_outdoorHumidityTextLabel = new QLabel("Humidity");
m_outdoorHumidityTextLabel->setFont(text);
m_outdoorHumidityTextLabel->setAlignment(Qt::AlignCenter);
m_luxTextLabel = new QLabel("Brightness");
m_luxTextLabel->setFont(text);
m_luxTextLabel->setAlignment(Qt::AlignCenter);
m_uvIndexTextLabel = new QLabel("UV Index");
m_uvIndexTextLabel->setFont(text);
m_uvIndexTextLabel->setAlignment(Qt::AlignCenter);
m_rainTodayTextLabel = new QLabel("Rainfall Today");
m_rainTodayTextLabel->setFont(text);
m_rainTodayTextLabel->setAlignment(Qt::AlignCenter);
m_totalRainTextLabel = new QLabel();
m_totalRainTextLabel->setFont(text);
m_totalRainTextLabel->setAlignment(Qt::AlignCenter);
m_airPressureMercuryTextLabel = new QLabel("Air Pressure");
m_airPressureMercuryTextLabel->setFont(text);
m_airPressureMercuryTextLabel->setAlignment(Qt::AlignCenter);
m_airPressureTrendTextLabel = new QLabel("Air Pressure Trend");
m_airPressureTrendTextLabel->setFont(text);
m_airPressureTrendTextLabel->setAlignment(Qt::AlignCenter);
m_layout = new QGridLayout(this);
m_layout->addWidget(m_outdoorTemperatureTextLabel, 0, 0);
m_layout->addWidget(m_outdoorTemperatureLabel, 1, 0);
m_layout->addWidget(m_outdoorHumidityTextLabel, 0, 1);
m_layout->addWidget(m_outdoorHumidityLabel, 1, 1);
m_layout->addWidget(m_airPressureMercuryTextLabel, 0, 2);
m_layout->addWidget(m_airPressureMercuryLabel, 1, 2);
m_layout->addWidget(m_luxTextLabel, 2, 0);
m_layout->addWidget(m_luxLabel, 3, 0);
m_layout->addWidget(m_uvIndexTextLabel, 2, 1);
m_layout->addWidget(m_uvIndexLabel, 3, 1);
m_layout->addWidget(m_airPressureTrendTextLabel, 2, 2);
m_layout->addWidget(m_airPressureTrendLabel, 3, 2);
m_layout->addWidget(m_rainTodayTextLabel, 4, 0);
m_layout->addWidget(m_rainTodayLabel, 5, 0);
m_layout->addWidget(m_totalRainTextLabel, 4, 1);
m_layout->addWidget(m_rainTodayLabel, 5, 1);
setLayout(m_layout);
m_totalRainTextLabel->setText(QString("%1 Rainfall").arg(now.year()));
}
WeatherWidget::~WeatherWidget()
{
}
void WeatherWidget::showEvent(QShowEvent* event)
{
qDebug() << __PRETTY_FUNCTION__;
}
void WeatherWidget::hideEvent(QHideEvent* event)
{
qDebug() << __PRETTY_FUNCTION__;
}
main.cpp
#include "homeinfo.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
HomeInfo w;
app.setOverrideCursor(QCursor(Qt::BlankCursor));
w.setGeometry(0, 0, 800, 480);
w.show();
return app.exec();
}
Debug output
void HomeInfo::stackWidgetChanged(int) : Index 0 // constructor
virtual void WeatherWidget::showEvent(QShowEvent*) // constructor
virtual bool HomeInfo::event(QEvent*) // touchevent
void HomeInfo::stackWidgetChanged(int) : Index 1
virtual bool HomeInfo::event(QEvent*) //touchevent
void HomeInfo::stackWidgetChanged(int) : Index 2
virtual bool HomeInfo::event(QEvent*) // touchevent
virtual void WeatherWidget::hideEvent(QHideEvent*) //WeatherWidget gets hidden
void HomeInfo::stackWidgetChanged(int) : Index 0 // signal that index changed to HomeInfo
virtual void WeatherWidget::showEvent(QShowEvent*) // WeatherWidget gets another showEvent
virtual void WeatherWidget::hideEvent(QHideEvent*) // after X closes window
virtual void WeatherWidget::hideEvent(QHideEvent*) // after X closes window
*** Exited normally ***```
No, this was just a dumb mistake. I used autocomplete when I attempted to type setLux() in HomeInfo.cpp (not visible, I removed the MQTT functions), and what I got was setVisible(). This meant that every time I got a new value for how bright it is outside, it was setVisible(true) on the widget when the double was implicitly cast to true. I would have expected a warning from gcc, but either warnings are turned off, or it wasn't generated. Ah well, this was not a QT bug or odd unexpected behavior, it was a mistake in my code.

QFrame is not adjusting its size as per the size adjustment of parent widget

I created a widget class. This class will be maintaining child_1 of type QFrame(which is of type QWidget) and also a child_2 of type QGridLayout . child_2 will be adding child_1 to itself. The child_1 will be having a child of type QGridLayout and ultimately QGridLayout will be having some push buttons.
*I am unable to adjust the size of QFrame child(child_1)as per the adjustment of parent widget.
Also, I am trying to set the size of QFrame child that of parent. Even this isn't happening.
I have tried using methods setFrameRect(), setGeometry() and resize().
I have implemented evenhandler resizeEvent(QResizeEvent event). But while application is running, I am trying to resize the widget and I see that log isn't printing.
main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.h
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QGridLayout* p_mPanelLayout; // Instance of QGridLayout.
FrameDemo* framePtr;
};
widget.cpp
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
p_mPanelLayout = new QGridLayout(this);
framePtr = new FrameDemo(this);
framePtr->createPushButtonUtility();
p_mPanelLayout->addWidget(framePtr, 0, 0, Qt::AlignCenter);
}
Widget::~Widget()
{
delete ui;
}
framedemo.h
#define LOG(message){\
qDebug()<< __LINE__<<__FUNCTION__<<message<<"\n";\
}
class FrameDemo : public QFrame
{
Q_OBJECT
public:
FrameDemo(QWidget *parent = 0);
void createPushButtonUtility();
private:
QGridLayout* m_pLayout;
protected:
void resizeEvent(QResizeEvent * event);
};
framedemo.cpp
FrameDemo::FrameDemo(QWidget *parent):QFrame(parent)
{
m_pLayout = new QGridLayout(this);
// m_pLayout->setRowStretch(0, parent->width());
QRect defaultRect = rect();
QSize currentSize = size();
LOG(currentSize.height())
LOG(currentSize.width())
int xP1 = 0;
int yP1 = 0;
int xP2 = 0;
int yP2 = 0;
defaultRect.getCoords(&xP1, &yP1, &xP2, &yP2);
LOG(xP1)
LOG(yP1)
LOG(xP2)
LOG(yP2)
LOG(parent->width())
LOG(defaultRect.height())
// QRect newRect = QRect(xP1,yP1,parent->width(),defaultRect.height());
// QRect newRect = QRect(0, 0, 0, defaultRect.height());
// setFrameRect(newRect);
resize(QSize(400, currentSize.height()));
setFrameShape(QFrame::Box);
// setGeometry(0, 0, parent->width(), defaultRect.height());
}
void FrameDemo::createPushButtonUtility()
{
QPushButton *readButton = new QPushButton("Read", this);
m_pLayout->addWidget(readButton, 0, 0, Qt::AlignCenter);
QPushButton *writeButton = new QPushButton("Write", this);
m_pLayout->addWidget(writeButton, 0, 1, Qt::AlignCenter);
QPushButton *updateButton = new QPushButton("Update", this);
m_pLayout->addWidget(updateButton, 0, 2, Qt::AlignCenter);
}
void FrameDemo::resizeEvent(QResizeEvent *event)
{
LOG("resize")
}
I managed to fix the problem.
I missed to implement the event handler resizeEvent(QResizeEvent *event) in parent widget.
I implemented the same and called the resizeEvent of FrameDemo. Inside this event handler, I called setGeometry().

Why Qt::AlignTop doesn't work in QVBoxLayout that I use like main layout?

I have simple class that inherits QDialog, I add dynamically elements
and my elements are located in the center, but I want to add them at the top.
class CustomDialog : public QDialog {
Q_OBJECT
private:
QVBoxLayout *mainLayout;
CustomDialog()
{
mainLayout = new QVBoxLayout();
setLayout(mainLayout);
}
public:
void update()
{
QLabel* label = new QLabel("some text");
QVBoxLayout *verLayout = new QVBoxLayout;
verLayout->addStretch();
verLayout->setAlignment(Qt::AlignTop);
verLayout->addWidget(label, Qt::AlignTop);
mainLayout->setAlignment(Qt::AlignTop);
mainLayout->addLayout(verLayout, Qt::AlignTop);
}
};
What am I doing wrong? and why my dynamically added elements always in center?
I understand that you want to place it and that the top is shown, so you could use QSpacerItem to push it.
class CustomDialog : public QDialog {
Q_OBJECT
QVBoxLayout *mainLayout;
public:
CustomDialog(QWidget *parent=0): QDialog(parent)
{
mainLayout = new QVBoxLayout(this);
QSpacerItem *verticalSpacer = new QSpacerItem(20, 217, QSizePolicy::Minimum, QSizePolicy::Expanding);
mainLayout->addItem(verticalSpacer);
addWidgets("1");
addWidgets("2");
}
private:
void addWidgets(const QString &text)
{
QLabel* label = new QLabel(text);
QVBoxLayout *verLayout = new QVBoxLayout;
verLayout->addStretch();
verLayout->setAlignment(Qt::AlignTop);
verLayout->addWidget(label, Qt::AlignTop);
mainLayout->setAlignment(Qt::AlignTop);
mainLayout->insertLayout(mainLayout->count()-1, verLayout);
}
};
Or if you want it to have a reverse order you must insert it in the first position with:
mainLayout->insertLayout(0, verLayout);
Note: The use of addLayout is incorrect since the second parameter is stretch.

How to check the ButtonGroup Id and change the other button according to that ButtonGroup id

I have QButtonGroup in which i have added 5 QPushButton and have set the id for all QPushButton. Now when id is 2, i want to setSize of id 1 and 3 to (100,100).
QButtonGroup *button = new QButtonGroup;
button.addButton(button1,1);
button.addButton(button2,2);
..
..
button.addButton(button5,5);
Now i want when button2 has focus then i want to set its size to (150,150) and button1 and button3 size to (100,100).
I got it,so here is what you need to do:
In my Dlg.h
class MyPushButton : public QPushButton
{
public:
MyPushButton(QString ButtonName, QWidget *parent);
void focusInEvent(QFocusEvent* event);
void focusOutEvent(QFocusEvent* event);
};
In my Dlg.cpp:
MyPushButton::MyPushButton(QString ButtonName, QWidget *parent)
:QPushButton(ButtonName,parent)
{
}
void MyPushButton::focusInEvent(QFocusEvent* event)
{
this->setMinimumHeight(150);
this->setMinimumWidth(150);
}
void MyPushButton::focusOutEvent(QFocusEvent* event)
{
this->setMinimumHeight(100);
this->setMinimumWidth(100);
}
You dont need QButtonGroup.Now all you need to do is use "MyPushButton" class and set the default height and width of the buttons to 100 *100.Let me know if you have any doubts.
MyMainWindow.cpp ,its ctor:
MyMainWindow::MyMainWindow(QWidget *parent, Qt::WFlags flags)
:QMainWindow(parent, flags)
{
ui.setupUi(this);
this->setWindowTitle(QString::fromUtf8("MainWindow"));
this->resize(250, 250);
QWidget *centralWidget = new QWidget(this);
//Create QPushButtons
button1 = new MyPushButton("Button 1" , centralWidget);
button1->setMinimumHeight(100);
button1->setMinimumWidth(100);
button2 = new MyPushButton("Button 2" , centralWidget);
button2->setMinimumHeight(100);
button2->setMinimumWidth(100);
button3 = new MyPushButton("Button 3" , centralWidget);
button3->setMinimumHeight(100);
button3->setMinimumWidth(100);
button4 = new MyPushButton("Button 4" , centralWidget);
button4->setMinimumHeight(100);
button4->setMinimumWidth(100);
QHBoxLayout* layout = new QHBoxLayout(centralWidget);
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->setSizeConstraint(QLayout::SetNoConstraint);
this->setCentralWidget(centralWidget);
}
In MyMainWindow.h
class MyMainWindow: public QMainWindow
{
Q_OBJECT
public:
MyMainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
~MyMainWindow();
public slots:
void FileNew(int i);
void keyReleaseEvent(QKeyEvent *e);
private:
Ui::StClass ui;
MyPushButton* button1;
MyPushButton* button2;
MyPushButton* button3;
MyPushButton* button4;
};

Added QMenuBar in QDialog it showing but sub menus dosn't open

i added QMenuBar in QDialog using layout that is set in Designer and then manully adding the QMenuBar , when running the application and the QDialog runs i see the QMenuBar with only the menu header without the opening sub menu :
here is my code :
this is the header file generated from the Designer :
QT_BEGIN_NAMESPACE
class Ui_AutoDialog
{
public:
QVBoxLayout *verticalLayout;
QWidget *widget_menuBarHolder;
QVBoxLayout *verticalLayout_2;
QVBoxLayout *verticalLayout_menuBarLayOut;
QWebView *webView;
QWidget *widget;
void setupUi(QDialog *AutoDialog)
{
if (AutoDialog->objectName().isEmpty())
AutoDialog->setObjectName(QString::fromUtf8("AutoDialog"));
AutoDialog->resize(436, 365);
verticalLayout = new QVBoxLayout(AutoDialog);
verticalLayout->setSpacing(0);
verticalLayout->setContentsMargins(0, 0, 0, 0);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
widget_menuBarHolder = new QWidget(AutoDialog);
widget_menuBarHolder->setObjectName(QString::fromUtf8("widget_menuBarHolder"));
widget_menuBarHolder->setMinimumSize(QSize(0, 20));
verticalLayout_2 = new QVBoxLayout(widget_menuBarHolder);
verticalLayout_2->setSpacing(0);
verticalLayout_2->setContentsMargins(0, 0, 0, 0);
verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
verticalLayout_menuBarLayOut = new QVBoxLayout();
verticalLayout_menuBarLayOut->setSpacing(0);
verticalLayout_menuBarLayOut->setObjectName(QString::fromUtf8("verticalLayout_menuBarLayOut"));
verticalLayout_2->addLayout(verticalLayout_menuBarLayOut);
verticalLayout->addWidget(widget_menuBarHolder);
webView = new QWebView(AutoDialog);
webView->setObjectName(QString::fromUtf8("webView"));
webView->setUrl(QUrl("about:blank"));
verticalLayout->addWidget(webView);
widget = new QWidget(AutoDialog);
widget->setObjectName(QString::fromUtf8("widget"));
widget->setMinimumSize(QSize(0, 20));
verticalLayout->addWidget(widget);
retranslateUi(AutoDialog);
QMetaObject::connectSlotsByName(AutoDialog);
} // setupUi
void retranslateUi(QDialog *AutoDialog)
{
AutoDialog->setWindowTitle(QApplication::translate("AutoDialog", "Dialog", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class AutoDialog: public Ui_AutoDialog {};
} // namespace Ui
QT_END_NAMESPACE
and here is the QDialog constructor , the part where is building the QMenuBAr:
ui.setupUi(this);
/*ui.verticalLayout_menuBarLayOut->addWidget(SetMenuBar());
ui.verticalLayout_menuBarLayOut->setMargin(0);
ui.verticalLayout_menuBarLayOut->setSpacing(0);*/
m_actionClose = new QAction(this);
m_actionClose->setObjectName(QString::fromUtf8("actionClose"));
m_actionPreferences = new QAction(this);
m_actionPreferences->setObjectName(QString::fromUtf8("actionPreferences"));
m_menubar = new QMenuBar(this);
m_menubar->setObjectName(QString::fromUtf8("menubar"));
m_menubar->setGeometry(QRect(0, 0, 314, 18));
m_menuFile = new QMenu(m_menubar);
m_menuFile->setObjectName(QString::fromUtf8("menuFile"));
m_menuSettings = new QMenu(m_menubar);
m_menuSettings->setObjectName(QString::fromUtf8("menuSettings"));
m_menubar->setMinimumSize(QSize(0, 20));
ui.verticalLayout_menuBarLayOut->setMenuBar(m_menubar);
m_menubar->addAction(m_menuFile->menuAction());
m_menubar->addAction(m_menuSettings->menuAction());
m_menuFile->addSeparator();
m_menuFile->addAction(m_actionClose);
m_menuSettings->addAction(m_actionPreferences);
m_actionClose->setText(QApplication::translate("MainWindow", "Close", 0, QApplication::UnicodeUTF8));
m_actionPreferences->setText(QApplication::translate("MainWindow", "Preferences", 0, QApplication::UnicodeUTF8));
m_menuFile->setTitle(QApplication::translate("MainWindow", "File", 0, QApplication::UnicodeUTF8));
m_menuSettings->setTitle(QApplication::translate("MainWindow", "Settings", 0, QApplication::UnicodeUTF8));
bar = new QStatusBar(this);
bar->setMinimumSize(QSize(0, 20));
ui.verticalLayout->removeWidget(ui.widget);
ui.verticalLayout->addWidget(bar);
ui.verticalLayout->setMargin(0);
ui.verticalLayout->setSpacing(0);
pb = new QProgressBar(bar);
pb->setTextVisible(false);
pb->hide();
bar->addPermanentWidget(pb);
what is wrong here why i can't see the sub menus.. Close & Preferences?
I'm not familiar with the way you're adding the menus, because I usually add menus to a menubar in a different way.
Try
m_menubar->addMenu( m_menuFile );
m_menubar->addMenu( m_menuSettings );
instead of
m_menubar->addAction(m_menuFile->menuAction());
m_menubar->addAction(m_menuSettings->menuAction());
and let me know if that worked. If not, there must be another problem I will look into.