What I want to do is that it give me a list with all .cpp files in the Directory and the Subdirectories. Problem it.HasNext stay on false and also it.filePath stay empty.
main.cpp
#include "QtSignal_Slot.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtSignal_Slot w;
w.show();
return a.exec();
}
ui_QtSignal_Slot.h
#ifndef UI_QTSIGNAL_SLOT_H
#define UI_QTSIGNAL_SLOT_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_QtSignal_SlotClass
{
public:
QWidget *centralWidget;
QGridLayout *gridLayout;
QPushButton *ButtonStart;
QSpacerItem *horizontalSpacer_2;
QSpacerItem *horizontalSpacer_4;
QTextEdit *textEdit;
QSpacerItem *verticalSpacer_2;
QSpacerItem *verticalSpacer_3;
QSpacerItem *horizontalSpacer;
QSpacerItem *horizontalSpacer_3;
QLineEdit *PathEdit;
QMenuBar *menuBar;
QToolBar *mainToolBar;
QStatusBar *statusBar;
void setupUi(QMainWindow *QtSignal_SlotClass)
{
if (QtSignal_SlotClass->objectName().isEmpty())
QtSignal_SlotClass->setObjectName(QStringLiteral("QtSignal_SlotClass"));
QtSignal_SlotClass->setEnabled(true);
QtSignal_SlotClass->resize(380, 250);
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(QtSignal_SlotClass->sizePolicy().hasHeightForWidth());
QtSignal_SlotClass->setSizePolicy(sizePolicy);
QtSignal_SlotClass->setMinimumSize(QSize(380, 250));
QtSignal_SlotClass->setMaximumSize(QSize(700, 300));
QtSignal_SlotClass->setSizeIncrement(QSize(400, 250));
QtSignal_SlotClass->setBaseSize(QSize(200, 250));
centralWidget = new QWidget(QtSignal_SlotClass);
centralWidget->setObjectName(QStringLiteral("centralWidget"));
gridLayout = new QGridLayout(centralWidget);
gridLayout->setSpacing(6);
gridLayout->setContentsMargins(11, 11, 11, 11);
gridLayout->setObjectName(QStringLiteral("gridLayout"));
ButtonStart = new QPushButton(centralWidget);
ButtonStart->setObjectName(QStringLiteral("ButtonStart"));
ButtonStart->setStyleSheet(QStringLiteral("background-color: rgb(200,200,255); color: rgb(0,0,0);"));
gridLayout->addWidget(ButtonStart, 7, 1, 1, 1);
horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
gridLayout->addItem(horizontalSpacer_2, 7, 2, 1, 1);
horizontalSpacer_4 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
gridLayout->addItem(horizontalSpacer_4, 0, 2, 1, 1);
textEdit = new QTextEdit(centralWidget);
textEdit->setObjectName(QStringLiteral("textEdit"));
textEdit->setStyleSheet(QStringLiteral("color: rgb(0,0,0);"));
gridLayout->addWidget(textEdit, 3, 1, 1, 1);
verticalSpacer_2 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
gridLayout->addItem(verticalSpacer_2, 1, 1, 1, 1);
verticalSpacer_3 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
gridLayout->addItem(verticalSpacer_3, 2, 1, 1, 1);
horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
gridLayout->addItem(horizontalSpacer, 7, 0, 1, 1);
horizontalSpacer_3 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
gridLayout->addItem(horizontalSpacer_3, 0, 0, 1, 1);
PathEdit = new QLineEdit(centralWidget);
PathEdit->setObjectName(QStringLiteral("PathEdit"));
QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Preferred);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(PathEdit->sizePolicy().hasHeightForWidth());
PathEdit->setSizePolicy(sizePolicy1);
PathEdit->setMinimumSize(QSize(200, 30));
PathEdit->setStyleSheet(QStringLiteral("background-color: rgb(240,240,240); color: rgb(0,0,0);"));
gridLayout->addWidget(PathEdit, 0, 1, 1, 1);
QtSignal_SlotClass->setCentralWidget(centralWidget);
menuBar = new QMenuBar(QtSignal_SlotClass);
menuBar->setObjectName(QStringLiteral("menuBar"));
menuBar->setGeometry(QRect(0, 0, 380, 21));
QtSignal_SlotClass->setMenuBar(menuBar);
mainToolBar = new QToolBar(QtSignal_SlotClass);
mainToolBar->setObjectName(QStringLiteral("mainToolBar"));
QtSignal_SlotClass->addToolBar(Qt::TopToolBarArea, mainToolBar);
statusBar = new QStatusBar(QtSignal_SlotClass);
statusBar->setObjectName(QStringLiteral("statusBar"));
QtSignal_SlotClass->setStatusBar(statusBar);
retranslateUi(QtSignal_SlotClass);
QMetaObject::connectSlotsByName(QtSignal_SlotClass);
} // setupUi
void retranslateUi(QMainWindow *QtSignal_SlotClass)
{
QtSignal_SlotClass->setWindowTitle(QApplication::translate("QtSignal_SlotClass", "QtSignal_Slot", Q_NULLPTR));
ButtonStart->setText(QApplication::translate("QtSignal_SlotClass", "Generate graphicfile", Q_NULLPTR));
PathEdit->setText(QString());
} // retranslateUi
};
namespace Ui {
class QtSignal_SlotClass: public Ui_QtSignal_SlotClass {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_QTSIGNAL_SLOT_H
QtSignal_Slot.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtSignal_Slot.h"
class QtSignal_Slot : public QMainWindow
{
Q_OBJECT
public:
QString m_Path;
QtSignal_Slot(QWidget *parent = Q_NULLPTR);
public slots:
void m_StartButtonpressed();
private:
Ui::QtSignal_SlotClass ui;
};
QtSolts_Signel.cpp
#include "QtSignal_Slot.h"
#include <ui_QtSignal_Slot.h>
#include <QDirIterator>
#include <QDebug>
QtSignal_Slot::QtSignal_Slot(QWidget *parent) : QMainWindow(nullptr)
{
ui.setupUi(this);
connect(ui.ButtonStart, SIGNAL(clicked()), this, SLOT(m_StartButtonpressed()));
}
void QtSignal_Slot::m_StartButton()
{
m_Path = (ui.PathEdit->text());
QDirIterator it(m_Path, QStringList() << "*.cpp" , QDir::Files, QDirIterator::Subdirectories);
do
{
qDebug() << it.next();
} while (it.hasNext());
qDebug() << "This, show in the Output";
QString Buffer = QString("%1").arg(QString::number(it.hasNext()));
ui.textEdit->setText(Buffer +"->" + it.filePath() + "<-" + m_Path);
}
The output in the textEdit is:
"0-> <-(correct path i but i as input)"
and output in the debug-output is:
" ' ' , This, show in the Output"
The code is updated, but the problem stays.
Example
Here is an example of how to make the slot work:
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QLineEdit;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
private:
QLineEdit *m_pathEdit;
QLineEdit *m_textEdit;
private slots:
void onButtonClicked();
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "MainWindow.h"
#include <QDirIterator>
#include <QBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
m_pathEdit(new QLineEdit(this)),
m_textEdit(new QLineEdit(this))
{
auto *widget = new QWidget(this);
auto *layoutMain = new QVBoxLayout(widget);
auto *button = new QPushButton(tr("Click me"), this);
layoutMain->addWidget(m_pathEdit);
layoutMain->addWidget(m_textEdit);
layoutMain->addWidget(button);
setCentralWidget(widget);
connect(button, &QPushButton::clicked, this, &MainWindow::onButtonClicked);
}
void MainWindow::onButtonClicked()
{
const QString &path(m_pathEdit->text());
QDirIterator it(path, QStringList() << "*.cpp" , QDir::Files, QDirIterator::Subdirectories);
do
{
qDebug() << it.next();
} while (it.hasNext());
qDebug() << "This, show in the Output";
QString buffer(QString("%1").arg(QString::number(it.hasNext())));
m_textEdit->setText(buffer +"->" + it.filePath() + "<-" + path);
}
Result
For the following input (entered in m_pathEdit, the upper QLineEdit):
/Users/Michael Scopchanov/Desktop/untitled9/
the result in the console is:
"/Users/Michael Scopchanov/Desktop/untitled9/main.cpp"
"/Users/Michael Scopchanov/Desktop/untitled9/MainWindow.cpp"
"/Users/Michael Scopchanov/Desktop/untitled9/MyLabel.cpp"
This, show in the Output
and the text of m_textEdit is set to:
0->/Users/Michael Scopchanov/Desktop/untitled9/MyLabel.cpp<-/Users/Michael Scopchanov/Desktop/untitled9/
Related
I am using QGridLayout, the first row has a QLabel which is used to show an icon set to 32x32 pixels. The next row has two QSvgWidgets, each of these is 16x14.
My code:
QGridLayout* pgrdloStatus(new QGridLayout);
if ( mplblStsIcon == nullptr )
{
mplblStsIcon = new QLabel();
}
if ( mpsvgRxIcon == nullptr )
{
mpsvgRxIcon = new QSvgWidget(":/SVG_LED");
mpsvgRxIcon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
mpsvgRxIcon->setFixedSize(TraineeMonitor::mscuintCommsIconWidth,
TraineeMonitor::mscuintCommsIconHeight);
}
if ( mpsvgTxIcon == nullptr )
{
mpsvgTxIcon = new QSvgWidget(":/SVG_LED");
mpsvgTxIcon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
mpsvgTxIcon->setFixedSize(TraineeMonitor::mscuintCommsIconWidth,
TraineeMonitor::mscuintCommsIconHeight);
}
const QString cstrToolTip(QString(
" %1: %2\r\n%3: %4")
.arg(tr("Hostname:")).arg(mstrHostname)
.arg(tr("MAC address:")).arg(mstrMACaddress));
mplblStsIcon->setToolTip(cstrToolTip);
pgrdloStatus->addWidget(mplblStsIcon, 0, 0, 1, 2, Qt::AlignHCenter);
pgrdloStatus->addWidget(mpsvgRxIcon, 1, 0, Qt::AlignLeft);
pgrdloStatus->addWidget(mpsvgTxIcon, 1, 1, Qt::AlignRight);
pgrdloStatus->setMargin(0);
pgrdloStatus->setSpacing(0);
return pgrdloStatus;
The result:
What I actually want is:
I create this Example and add Items From the Designer to show you why you see that distance between icons.
I create GridLayout and put 2 labels and set 2 LED SVG image
this is what actually you do too :
But For Fixing this issue you should check this property :
which means this : label->setScaledContents(true);
Which means that your icons match to labels.
For removing margins of GridLay out you can also do this :
This is its code:
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public:
QWidget *centralwidget;
QGridLayout *gridLayout;
QLabel *label;
QLabel *label_2;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(207, 109);
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
gridLayout = new QGridLayout(centralwidget);
gridLayout->setSpacing(0);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
gridLayout->setContentsMargins(0, 0, 0, 0);
label = new QLabel(centralwidget);
label->setObjectName(QString::fromUtf8("label"));
label->setPixmap(QPixmap(QString::fromUtf8(":/icons/led-square-red.svg")));
label->setScaledContents(true);
gridLayout->addWidget(label, 0, 0, 1, 1);
label_2 = new QLabel(centralwidget);
label_2->setObjectName(QString::fromUtf8("label_2"));
label_2->setPixmap(QPixmap(QString::fromUtf8(":/icons/led-square-red.svg")));
label_2->setScaledContents(true);
gridLayout->addWidget(label_2, 0, 1, 1, 1);
MainWindow->setCentralWidget(centralwidget);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr));
label->setText(QString());
label_2->setText(QString());
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MAINWINDOW_H
I am working on a project in which I am supposed to make two buttons(These buttons are named as Power calculation and Power calculation 2 ). When the user clicks on the buttons, a sliding area opens up under the buttons.
I have written this code successfully, but my problem is the layout of these buttons and the sliding area bellow each button(The buttons are defined in separate classes. The button named as "Power calculation" is defined in "Section.cpp" and the button named as "Power calculation 2" is defined in "Section2.cpp").
My goal is to set the buttons in the same row (the button named as "power calculation" must be on the left side of the button named as "Powercalculation2" and both of them are supposed to be in the same row). I use QGridLayout to set the layouts to my elements, but I can't get results. I have also tried to use move() and setGeometry() , but I could not get what I want to do.
I would appreciate if someone could help me in this matter.
The picture below is what I get after running my program:
But the result that I want to get is the picture below:
MY project ui file picture:
Here is my code:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Section.h:
#ifndef SECTION_H
#define SECTION_H
#include <QFrame>
#include <QGridLayout>
#include <QParallelAnimationGroup>
#include <QScrollArea>
#include <QGridLayout>
#include <QParallelAnimationGroup>
#include <QScrollArea>
#include <QToolButton>
#include <QWidget>
#include "Section2.h"
class Section : public QWidget {
Q_OBJECT
private:
QGridLayout* mainLayout;
QToolButton* toggleButton;
QFrame* headerLine;
QParallelAnimationGroup* toggleAnimation;
QScrollArea* contentArea;
int animationDuration;
public slots:
void toggle(bool collapsed);
public:
explicit Section(const QString & title = "", const int animationDuration = 100, QWidget* parent =
0);
void setContentLayout(QLayout & contentLayout);
};
#endif // SECTION_H
Section2.h:
#ifndef SECTION2_H
#define SECTION2_H
#include <QFrame>
#include <QGridLayout>
#include <QParallelAnimationGroup>
#include <QScrollArea>
#include <QGridLayout>
#include <QParallelAnimationGroup>
#include <QScrollArea>
#include <QToolButton>
#include <QWidget>
class Section2 : public QWidget {
Q_OBJECT
private:
QGridLayout* mainLayout;
QToolButton* toggleButton;
QFrame* headerLine;
QParallelAnimationGroup* toggleAnimation;
QScrollArea* contentArea;
int animationDuration;
public slots:
void toggle(bool collapsed);
public:
explicit Section2(const QString & title = "", const int animationDuration = 100, QWidget* parent =
0);
void setContentLayout(QLayout & contentLayout);
};
#endif // SECTION_H
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Section.h"
#include "Section2.h"
#include <QLabel>
#include <QPushButton>
#include <QBoxLayout>
#include <QLineEdit>
#include <QGridLayout>
#include <QComboBox>
#include <QDebug>
#include <QRegularExpressionValidator>
#include <QValidator>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Section *section = new Section("Section", 100, ui->centralWidget);
ui->centralWidget->layout()->addWidget(section);
Section2 *section2 = new Section2("Section2", 100, ui->centralWidget);
ui->centralWidget->layout()->addWidget(section2);
QVBoxLayout* anyLayout = new QVBoxLayout();
QLabel *lblV = new QLabel("insert voltage",section);
anyLayout->addWidget(lblV);
QLineEdit *lineV = new QLineEdit("",section);
anyLayout->addWidget(lineV);
QLabel *lblI = new QLabel("insert current",section);
anyLayout->addWidget(lblI);
QLineEdit *lineI = new QLineEdit("",section);
anyLayout->addWidget(lineI);
QPushButton *btn = new QPushButton("power: ",section);
anyLayout->addWidget(btn);
QLabel *lbl = new QLabel("",section);
lbl->setStyleSheet("background-color: yellow");
anyLayout->addWidget(lbl);
QVBoxLayout* anyLayout2 = new QVBoxLayout();
QLabel *lblV2 = new QLabel("insert voltage",section2);
anyLayout2->addWidget(lblV2);
QLineEdit *lineV2 = new QLineEdit("",section2);
anyLayout2->addWidget(lineV2);
QLabel *lblI2 = new QLabel("insert current",section2);
anyLayout2->addWidget(lblI2);
QLineEdit *lineI2 = new QLineEdit("",section2);
anyLayout2->addWidget(lineI2);
QPushButton *btn2 = new QPushButton("power: ",section2);
anyLayout2->addWidget(btn2);
QLabel *lbl2 = new QLabel("",section2);
lbl2->setStyleSheet("background-color: pink");
anyLayout2->addWidget(lbl2);
section->setContentLayout(*anyLayout);
section2->setContentLayout(*anyLayout2);
}
MainWindow::~MainWindow()
{
delete ui;
}
Section.cpp:
#include <QPropertyAnimation>
#include "Section.h"
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
Section::Section(const QString & title, const int animationDuration, QWidget* parent)
: QWidget(parent), animationDuration(animationDuration)
{
toggleButton = new QToolButton(this);
headerLine = new QFrame(this);
toggleAnimation = new QParallelAnimationGroup(this);
contentArea = new QScrollArea(this);
mainLayout = new QGridLayout(this);
toggleButton->setStyleSheet("QToolButton {border: none;}");
toggleButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toggleButton->setArrowType(Qt::ArrowType::RightArrow);
toggleButton->setText("Power calculation");
toggleButton->setCheckable(true);
toggleButton->setChecked(false);
headerLine->setFrameShape(QFrame::HLine);
headerLine->setFrameShadow(QFrame::Sunken);
headerLine->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
contentArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
// start out collapsed
contentArea->setMaximumHeight(0);
toggleAnimation->addAnimation(new QPropertyAnimation(contentArea, "maximumHeight"));
mainLayout->setVerticalSpacing(0);
mainLayout->addWidget(toggleButton, 0, 0, 1, 1);
mainLayout->addWidget(headerLine, 0, 1, 1, 1);
mainLayout->addWidget(contentArea, 3, 0, 1, 3);
setLayout(mainLayout);
connect(toggleButton, &QToolButton::toggled, this, &Section::toggle);
}
void Section::toggle(bool collapsed) {
toggleButton->setArrowType(collapsed ? Qt::ArrowType::DownArrow : Qt::ArrowType::RightArrow);
toggleAnimation->setDirection(collapsed ? QAbstractAnimation::Forward :
QAbstractAnimation::Backward);
toggleAnimation->start();
}
void Section::setContentLayout(QLayout & contentLayout)
{
delete contentArea->layout();
contentArea->setLayout(&contentLayout);
const auto collapsedHeight = sizeHint().height() - contentArea->maximumHeight();
auto contentHeight = contentLayout.sizeHint().height();
for (int i = 0; i < toggleAnimation->animationCount() - 1; ++i)
{
QPropertyAnimation* SectionAnimation = static_cast<QPropertyAnimation *>(toggleAnimation->animationAt(i));
SectionAnimation->setDuration(animationDuration);
SectionAnimation->setStartValue(collapsedHeight);
SectionAnimation->setEndValue(collapsedHeight + contentHeight);
}
QPropertyAnimation* contentAnimation = static_cast<QPropertyAnimation *>(toggleAnimation->animationAt(toggleAnimation->animationCount() - 1));
contentAnimation->setDuration(animationDuration);
contentAnimation->setStartValue(0);
contentAnimation->setEndValue(contentHeight);
}
Section2.cpp:
#include <QPropertyAnimation>
#include "Section2.h"
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
Section2::Section2(const QString & title, const int animationDuration, QWidget* parent)
: QWidget(parent), animationDuration(animationDuration)
{
toggleButton = new QToolButton(this);
headerLine = new QFrame(this);
toggleAnimation = new QParallelAnimationGroup(this);
contentArea = new QScrollArea(this);
mainLayout = new QGridLayout(this);
toggleButton->setStyleSheet("QToolButton {border: none;}");
toggleButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toggleButton->setArrowType(Qt::ArrowType::RightArrow);
toggleButton->setText("Power calculation 2");
toggleButton->setCheckable(true);
toggleButton->setChecked(false);
headerLine->setFrameShape(QFrame::HLine);
headerLine->setFrameShadow(QFrame::Sunken);
headerLine->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
contentArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
// start out collapsed
contentArea->setMaximumHeight(0);
toggleAnimation->addAnimation(new QPropertyAnimation(contentArea, "maximumHeight"));
mainLayout->setVerticalSpacing(0);
mainLayout->addWidget(toggleButton, 0, 0, 1, 1);
mainLayout->addWidget(headerLine, 0, 1, 1, 1);
mainLayout->addWidget(contentArea, 3, 0, 1, 3);
setLayout(mainLayout);
connect(toggleButton, &QToolButton::toggled, this, &Section2::toggle);
}
void Section2::toggle(bool collapsed) {
toggleButton->setArrowType(collapsed ? Qt::ArrowType::DownArrow : Qt::ArrowType::RightArrow);
toggleAnimation->setDirection(collapsed ? QAbstractAnimation::Forward :
QAbstractAnimation::Backward);
toggleAnimation->start();
}
void Section2::setContentLayout(QLayout & contentLayout)
{
delete contentArea->layout();
contentArea->setLayout(&contentLayout);
const auto collapsedHeight = sizeHint().height() - contentArea->maximumHeight();
auto contentHeight = contentLayout.sizeHint().height();
for (int i = 0; i < toggleAnimation->animationCount() - 1; ++i)
{
QPropertyAnimation* SectionAnimation = static_cast<QPropertyAnimation *>(toggleAnimation-
>animationAt(i));
SectionAnimation->setDuration(animationDuration);
SectionAnimation->setStartValue(collapsedHeight);
SectionAnimation->setEndValue(collapsedHeight + contentHeight);
}
QPropertyAnimation* contentAnimation = static_cast<QPropertyAnimation *>(toggleAnimation-
>animationAt(toggleAnimation->animationCount() - 1));
contentAnimation->setDuration(animationDuration);
contentAnimation->setStartValue(0);
contentAnimation->setEndValue(contentHeight);
}
You have 2 widgets (Section and Section2) that contain other widgets (buttons/labels) that you want to be placed horizontally in your main window (inside centralWidget).
For that you need to set the appropriate layout to the centralWidget and add Section* items to that layout.
Right now, you just use the default layout of the centralWidget:
Section *section = new Section("Section", 100, ui->centralWidget);
ui->centralWidget->layout()->addWidget(section);
Section2 *section2 = new Section2("Section2", 100, ui->centralWidget);
ui->centralWidget->layout()->addWidget(section2);
I use QGridLayout to set the layouts to my elements, but I can't get results.
This is because in your code you use QGridLayout to lay out the widgets inside each instance of Section and Section2.
mainLayout->addWidget(toggleButton, 0, 0, 1, 1);
mainLayout->addWidget(headerLine, 0, 1, 1, 1);
mainLayout->addWidget(contentArea, 3, 0, 1, 3);
setLayout(mainLayout);
This controls how toggleButton, headerLine and contentArea are laid out inside Section* widgets, but has no effect on how Section widgets itself are positioned with respect to each other in some "higher level" widget (centralWidget in this case).
When you create your Section* widgets you can think of them as any other standard widget (button/label/etc.) and handle them accordingly.
I'm inside a class which deals with a simple window (I'm starting with Qt) but when I try to add a new Widget (from a slot of the custom class) to this window, it doesn't appear on the window. I use QTextStream which shows that the slot is correctly called and the random coordinates generated are rights so I totally do not understand why there is nothing new on my window. Moreover, the first time the slot is called as a function in the ButtonsWindow::ButtonsWindow(QWidget *parent) : QWidget(parent) {}, the widget appears correctly... Do you have any ideas ?
Here is my .h code :
#ifndef BUTTONSWINDOW_H
#define BUTTONSWINDOW_H
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include <cstdlib>
#include <ctime>
#include <QDebug>
#include <QVector>
class ButtonsWindow : public QWidget
{
Q_OBJECT
public:
explicit ButtonsWindow(QWidget *parent = nullptr);
private:
QPushButton *testButton;
QVector<QWidget*> buttonsArray;
signals:
public slots:
void createNewButton();
};
#endif // BUTTONSWINDOW_H
Here is my .cpp class code :
#include "buttonswindow.h"
ButtonsWindow::ButtonsWindow(QWidget *parent) : QWidget(parent)
{
setFixedSize(1000, 300);
createNewButton();
}
void ButtonsWindow::createNewButton()
{
QWidget *mainWidget = new QWidget(this);
QPalette pal = palette();
pal.setColor(QPalette::Background, Qt::lightGray);
mainWidget->setAutoFillBackground(true);
mainWidget->setPalette(pal);
QGridLayout *layout = new QGridLayout;
QLabel *text = new QLabel("What to do?");
text->setCursor(Qt::WhatsThisCursor);
QPushButton *addWidget = new QPushButton("Create a new Button");
QObject::connect(addWidget, SIGNAL(pressed()), this, SLOT(createNewButton()));
addWidget->setCursor(Qt::CrossCursor);
QPushButton *removeWidget = new QPushButton("Remove an existing random Button");
removeWidget->setCursor(Qt::ForbiddenCursor);
layout->addWidget(text, 0, 0, 1, 2, Qt::AlignCenter);
layout->addWidget(addWidget, 1, 0, 1, 1, Qt::AlignCenter);
layout->addWidget(removeWidget, 1, 1, 1, 1, Qt::AlignCenter);
mainWidget->setLayout(layout);
int nX = (rand() % this->width() - 2 * mainWidget->width());
nX += mainWidget->width();
int nY = (rand() % this->height() - 2 * mainWidget->height());
nY += mainWidget->height();
mainWidget->move(nX, nY);
QTextStream(stdout) << "check " << nX << " : : " << nY;
buttonsArray.append(mainWidget);
}
Finnaly, here is my main.cpp code :
#include <QApplication>
#include <QPushButton>
#include "buttonswindow.h"
#include <QGridLayout>
using namespace std;
int main(int argc, char *argv[]) {
srand(time(0));
QApplication app(argc, argv);
ButtonsWindow window;
window.show();
return app.exec();
}
From the documentation...
If you add a child widget to an already visible widget you must
explicitly show the child to make it visible.
So you just need to add
mainWidget->show();
at the end of your ButtonsWindow::createNewButton implementation.
When I press the close button on the tab widget, that tab must close, and the corresponding member of the identifyedWidgetMap must be copied into the deletedWidgetMap. However, the copying does not happen.
For instance, I close Tab_3 but there is in the deletedWidgetMap some information: 4, QWidget(0x17424a1).
Help if you can. Thank you.
That code lines I created are located below.
Header
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QTabWidget>
#include <QVBoxLayout>
#include <QWidget>
#include <QTextEdit>
#include <QMap>
#include <QList>
#include <iostream>
#include <QDebug>
#include <QString>
#include <QFont>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void setMainWindowPropertiesAndItems();
QMenuBar* menuBar;
QMenu* fileMenu;
QAction* newTabAction;
QAction* openAction;
QTabWidget* tabWidget;
QMap<int, QWidget*> identifyedWidgetMap;
QMap<int, QWidget*> deletedWidgetMap;
QList<QTextEdit*> textEditList;
QVBoxLayout* vBoxLayout;
private slots:
void newTabActionHandler();
void openActionHandler();
void tryingToCloseConcreteTab(int);
};
#endif // MAINWINDOW_H
Source
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setMainWindowPropertiesAndItems();
}
MainWindow::~MainWindow()
{
}
void MainWindow::setMainWindowPropertiesAndItems()
{
setWindowTitle("Notepad");
setGeometry(0, 0, 500, 250);
move(270, 270);
menuBar = new QMenuBar(this);
setMenuBar(menuBar);
fileMenu = new QMenu("&File", this);
menuBar->addMenu(fileMenu);
newTabAction = new QAction("&New Tab", this);
fileMenu->addAction(newTabAction);
connect(newTabAction, SIGNAL(triggered()), this,
SLOT(newTabActionCommandsHandler()));
openAction = new QAction("&Open", this);
fileMenu->addAction(openAction);
connect(openAction, SIGNAL(triggered()), this,
SLOT(openActionCommandsHandler()));
identifyedWidgetMap.insert(0, new QWidget(this));
textEditList.append(new QTextEdit(this));
tabWidget = new QTabWidget(this);
tabWidget->addTab(identifyedWidgetMap.value(0), QString("Tab
%1").arg(identifyedWidgetMap.size()-1));
tabWidget->setMovable(true);
tabWidget->setTabsClosable(true);
connect(tabWidget, SIGNAL(tabCloseRequested(int)), this,
SLOT(tryingToCloseConcreteTab(int)));
vBoxLayout = new QVBoxLayout();
identifyedWidgetMap.value(0)->setLayout(vBoxLayout);
vBoxLayout->addWidget(textEditList[0]);
textEditList[0]->setCurrentFont(QFont("Monospace Regular", 14));
setCentralWidget(tabWidget);
}
void MainWindow::newTabActionCommandsHandler()
{
if(bool(deletedWidgetMap.isEmpty()) == true)
{
identifyedWidgetMap.insert(identifyedWidgetMap.size(), new
QWidget(this));
textEditList.append(new QTextEdit(this));
tabWidget->
addTab(identifyedWidgetMap.value(identifyedWidgetMap.size()-1),
QString("Tab %1").arg(identifyedWidgetMap.size()-1));
tabWidget->
setCurrentWidget(identifyedWidgetMap.
value(identifyedWidgetMap.size()-1));
vBoxLayout = new QVBoxLayout();
identifyedWidgetMap.value(identifyedWidgetMap.size()-1)->
setLayout(vBoxLayout);
vBoxLayout->addWidget(textEditList[textEditList.size()-1]);
textEditList[textEditList.size()-1]->setCurrentFont(QFont("Monospace
Regular", 14));
}
}
void MainWindow::openActionCommandsHandler()
{
QWidget* currentWidget = tabWidget->currentWidget();
std::cout<<"The currentWidget address is: "<<currentWidget<<std::endl;
qDebug()<<"The complete identifyedWidgetMap address set is: ";
foreach(int widgetIdentifyer, identifyedWidgetMap.keys())
{
qDebug()<<widgetIdentifyer<<", "
<<identifyedWidgetMap.value(widgetIdentifyer);
}
int currentWidgetIndex = 0;
currentWidgetIndex = tabWidget->currentIndex();
std::cout<<"The currentWidgetIndex is: "<<currentWidgetIndex<<std::endl;
qDebug()<<"The complete deletedWidgetMap set is: ";
foreach(int widgetIdentifyer, deletedWidgetMap.keys())
{
qDebug()<<widgetIdentifyer<<", "
<<deletedWidgetMap.value(widgetIdentifyer);
}
}
void MainWindow::tryingToCloseConcreteTab(int concreteIndex)
{
tabWidget->removeTab(concreteIndex);
std::cout<<"Deleted identifyer is: "<<concreteIndex<<std::endl;
deletedWidgetMap.insert(identifyedWidgetMap.key(tabWidget-
>widget(concreteIndex)),
identifyedWidgetMap.value(identifyedWidgetMap.key(tabWidget-
>widget(concreteIndex))));
}
The problem I see is you first remove the tab from the tabWidget. This results in an index change, the next tab will shift to left when you remove the tab. When you try to access the tab with tabWidget->widget(concreteIndex)you access the next tab.
You may consider adding tab to the deletedWidgetMap before removing it.
I'm trying to build a GUI app, and I'm doing this through Qt. I also want to create a multi window application: I want that when I hit a button the other window shows up ("hiding" the previous one). Is that a GDI?
So far, I have create a .ui file for every window I want (currently 4), and I'm trying to connect them that way (the main window, with the other 3).
How could I do that?
I'm sending the file of the program in order to make my problema more undestandable:
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
StudyWindow s;
QStackedWidget *stackedWidget = new QStackedWidget;
stackedWidget->addWidget(w);
stackedWidget->addWidget(s);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(stackedWidget);
setLayout(layout);
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ConnectStuff();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::ConnectStuff()
{
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QLayout>
#include <QStackedWidget>
#include "study.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
void ConnectStuff();
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
study.h
#ifndef STUDYWINDOW_H
#define STUDYWINDOW_H
#include <QMainWindow>
#include <QPushButton>
namespace Ui {
class StudyWindow;
}
class StudyWindow : public QMainWindow
{
Q_OBJECT
public:
explicit StudyWindow(QWidget *parent = 0);
~StudyWindow();
private:
Ui::StudyWindow *ui;
};
#endif // STUDYWINDOW_H
ui_Study.h
/********************************************************************************
** Form generated from reading UI file 'Study.ui'
**
** Created: Tue 20. Mar 20:10:56 2012
** by: Qt User Interface Compiler version 4.7.4
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_STUDY_H
#define UI_STUDY_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QTreeWidget>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_StudyWindow
{
public:
QAction *actionVoltar;
QAction *actionSair;
QWidget *centralwidget;
QTreeWidget *treeWidget;
QPushButton *pushButton;
QMenuBar *menubar;
QMenu *menuVoltar;
QStatusBar *statusbar;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(800, 600);
actionVoltar = new QAction(MainWindow);
actionVoltar->setObjectName(QString::fromUtf8("actionVoltar"));
actionSair = new QAction(MainWindow);
actionSair->setObjectName(QString::fromUtf8("actionSair"));
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
treeWidget = new QTreeWidget(centralwidget);
QFont font;
font.setPointSize(8);
font.setBold(true);
font.setWeight(75);
QTreeWidgetItem *__qtreewidgetitem = new QTreeWidgetItem();
__qtreewidgetitem->setFont(0, font);
treeWidget->setHeaderItem(__qtreewidgetitem);
new QTreeWidgetItem(treeWidget);
new QTreeWidgetItem(treeWidget);
new QTreeWidgetItem(treeWidget);
new QTreeWidgetItem(treeWidget);
new QTreeWidgetItem(treeWidget);
new QTreeWidgetItem(treeWidget);
new QTreeWidgetItem(treeWidget);
new QTreeWidgetItem(treeWidget);
new QTreeWidgetItem(treeWidget);
new QTreeWidgetItem(treeWidget);
treeWidget->setObjectName(QString::fromUtf8("treeWidget"));
treeWidget->setGeometry(QRect(0, 110, 161, 451));
pushButton = new QPushButton(centralwidget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(0, 0, 75, 23));
MainWindow->setCentralWidget(centralwidget);
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 800, 21));
menuVoltar = new QMenu(menubar);
menuVoltar->setObjectName(QString::fromUtf8("menuVoltar"));
MainWindow->setMenuBar(menubar);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);
menubar->addAction(menuVoltar->menuAction());
menuVoltar->addAction(actionVoltar);
menuVoltar->addSeparator();
menuVoltar->addAction(actionSair);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
actionVoltar->setText(QApplication::translate("MainWindow", "Voltar", 0, QApplication::UnicodeUTF8));
actionSair->setText(QApplication::translate("MainWindow", "Sair", 0, QApplication::UnicodeUTF8));
QTreeWidgetItem *___qtreewidgetitem = treeWidget->headerItem();
___qtreewidgetitem->setText(1, QApplication::translate("MainWindow", "Items", 0, QApplication::UnicodeUTF8));
___qtreewidgetitem->setText(0, QApplication::translate("MainWindow", "Mat\303\251ria", 0, QApplication::UnicodeUTF8));
const bool __sortingEnabled = treeWidget->isSortingEnabled();
treeWidget->setSortingEnabled(false);
QTreeWidgetItem *___qtreewidgetitem1 = treeWidget->topLevelItem(0);
___qtreewidgetitem1->setText(0, QApplication::translate("MainWindow", "Portugu\303\252s", 0, QApplication::UnicodeUTF8));
QTreeWidgetItem *___qtreewidgetitem2 = treeWidget->topLevelItem(1);
___qtreewidgetitem2->setText(0, QApplication::translate("MainWindow", "Reda\303\247\303\243o", 0, QApplication::UnicodeUTF8));
QTreeWidgetItem *___qtreewidgetitem3 = treeWidget->topLevelItem(2);
___qtreewidgetitem3->setText(0, QApplication::translate("MainWindow", "Matem\303\241tica", 0, QApplication::UnicodeUTF8));
QTreeWidgetItem *___qtreewidgetitem4 = treeWidget->topLevelItem(3);
___qtreewidgetitem4->setText(0, QApplication::translate("MainWindow", "Biologia", 0, QApplication::UnicodeUTF8));
QTreeWidgetItem *___qtreewidgetitem5 = treeWidget->topLevelItem(4);
___qtreewidgetitem5->setText(0, QApplication::translate("MainWindow", "F\303\255sica", 0, QApplication::UnicodeUTF8));
QTreeWidgetItem *___qtreewidgetitem6 = treeWidget->topLevelItem(5);
___qtreewidgetitem6->setText(0, QApplication::translate("MainWindow", "Qu\303\255mica", 0, QApplication::UnicodeUTF8));
QTreeWidgetItem *___qtreewidgetitem7 = treeWidget->topLevelItem(6);
___qtreewidgetitem7->setText(0, QApplication::translate("MainWindow", "Hist\303\263ria", 0, QApplication::UnicodeUTF8));
QTreeWidgetItem *___qtreewidgetitem8 = treeWidget->topLevelItem(7);
___qtreewidgetitem8->setText(0, QApplication::translate("MainWindow", "Geografia", 0, QApplication::UnicodeUTF8));
QTreeWidgetItem *___qtreewidgetitem9 = treeWidget->topLevelItem(8);
___qtreewidgetitem9->setText(0, QApplication::translate("MainWindow", "Ingl\303\252s", 0, QApplication::UnicodeUTF8));
QTreeWidgetItem *___qtreewidgetitem10 = treeWidget->topLevelItem(9);
___qtreewidgetitem10->setText(0, QApplication::translate("MainWindow", "Espanhol", 0, QApplication::UnicodeUTF8));
treeWidget->setSortingEnabled(__sortingEnabled);
pushButton->setText(QApplication::translate("MainWindow", "Cansei!", 0, QApplication::UnicodeUTF8));
menuVoltar->setTitle(QApplication::translate("MainWindow", "Arquivo", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class StudyWindow: public Ui_StudyWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_STUDY_H
ui_mainwindow.h
/********************************************************************************
** Form generated from reading UI file 'mainwindow.ui'
**
** Created: Tue 20. Mar 20:10:56 2012
** by: Qt User Interface Compiler version 4.7.4
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QGroupBox>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QPlainTextEdit>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public slots:
public:
QAction *actionAjuda;
QAction *actionLista_de;
QAction *actionSair;
QWidget *centralwidget;
QPushButton *pshBStudy;
QPushButton *pshBSimulator;
QPushButton *pshBExamCalen;
QPushButton *pshBReadOfDay;
QLabel *labelTitle;
QPlainTextEdit *plainTextNews;
QLabel *labelNews;
QGroupBox *groupBox;
QLabel *labelCollege;
QLabel *labelCourse;
QLabel *labelMemSince;
QLabel *labelLoggedWith;
QLabel *labelBP;
QStatusBar *statusbar;
QMenuBar *menuBar;
QMenu *menuArquivo;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(800, 600);
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(MainWindow->sizePolicy().hasHeightForWidth());
MainWindow->setSizePolicy(sizePolicy);
actionAjuda = new QAction(MainWindow);
actionAjuda->setObjectName(QString::fromUtf8("actionAjuda"));
actionLista_de = new QAction(MainWindow);
actionLista_de->setObjectName(QString::fromUtf8("actionLista_de"));
actionSair = new QAction(MainWindow);
actionSair->setObjectName(QString::fromUtf8("actionSair"));
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
pshBStudy = new QPushButton(centralwidget);
pshBStudy->setObjectName(QString::fromUtf8("pshBStudy"));
pshBStudy->setGeometry(QRect(140, 120, 161, 81));
pshBStudy->setDefault(true);
pshBSimulator = new QPushButton(centralwidget);
pshBSimulator->setObjectName(QString::fromUtf8("pshBSimulator"));
pshBSimulator->setGeometry(QRect(530, 120, 161, 81));
pshBExamCalen = new QPushButton(centralwidget);
pshBExamCalen->setObjectName(QString::fromUtf8("pshBExamCalen"));
pshBExamCalen->setGeometry(QRect(140, 260, 161, 81));
pshBReadOfDay = new QPushButton(centralwidget);
pshBReadOfDay->setObjectName(QString::fromUtf8("pshBReadOfDay"));
pshBReadOfDay->setGeometry(QRect(530, 260, 161, 81));
labelTitle = new QLabel(centralwidget);
labelTitle->setObjectName(QString::fromUtf8("labelTitle"));
labelTitle->setGeometry(QRect(200, 10, 431, 71));
QFont font;
font.setPointSize(23);
labelTitle->setFont(font);
plainTextNews = new QPlainTextEdit(centralwidget);
plainTextNews->setObjectName(QString::fromUtf8("plainTextNews"));
plainTextNews->setGeometry(QRect(610, 440, 181, 111));
plainTextNews->setReadOnly(true);
labelNews = new QLabel(centralwidget);
labelNews->setObjectName(QString::fromUtf8("labelNews"));
labelNews->setGeometry(QRect(610, 420, 81, 16));
groupBox = new QGroupBox(centralwidget);
groupBox->setObjectName(QString::fromUtf8("groupBox"));
groupBox->setGeometry(QRect(0, 460, 431, 91));
labelCollege = new QLabel(groupBox);
labelCollege->setObjectName(QString::fromUtf8("labelCollege"));
labelCollege->setGeometry(QRect(230, 50, 111, 16));
labelCourse = new QLabel(groupBox);
labelCourse->setObjectName(QString::fromUtf8("labelCourse"));
labelCourse->setGeometry(QRect(230, 30, 111, 16));
labelMemSince = new QLabel(groupBox);
labelMemSince->setObjectName(QString::fromUtf8("labelMemSince"));
labelMemSince->setGeometry(QRect(10, 50, 111, 16));
labelLoggedWith = new QLabel(groupBox);
labelLoggedWith->setObjectName(QString::fromUtf8("labelLoggedWith"));
labelLoggedWith->setGeometry(QRect(10, 30, 111, 16));
labelBP = new QLabel(groupBox);
labelBP->setObjectName(QString::fromUtf8("labelBP"));
labelBP->setGeometry(QRect(10, 70, 111, 16));
MainWindow->setCentralWidget(centralwidget);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);
menuBar = new QMenuBar(MainWindow);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
menuBar->setGeometry(QRect(0, 0, 800, 21));
menuArquivo = new QMenu(menuBar);
menuArquivo->setObjectName(QString::fromUtf8("menuArquivo"));
MainWindow->setMenuBar(menuBar);
QWidget::setTabOrder(pshBStudy, pshBSimulator);
QWidget::setTabOrder(pshBSimulator, pshBExamCalen);
QWidget::setTabOrder(pshBExamCalen, pshBReadOfDay);
QWidget::setTabOrder(pshBReadOfDay, plainTextNews);
menuBar->addAction(menuArquivo->menuAction());
menuArquivo->addAction(actionSair);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
actionAjuda->setText(QApplication::translate("MainWindow", "Sobre...", 0, QApplication::UnicodeUTF8));
actionLista_de->setText(QApplication::translate("MainWindow", "Lista de Provas", 0, QApplication::UnicodeUTF8));
actionSair->setText(QApplication::translate("MainWindow", "Sair...", 0, QApplication::UnicodeUTF8));
pshBStudy->setText(QApplication::translate("MainWindow", "Estudar!", 0, QApplication::UnicodeUTF8));
pshBSimulator->setText(QApplication::translate("MainWindow", "Simulado", 0, QApplication::UnicodeUTF8));
pshBExamCalen->setText(QApplication::translate("MainWindow", "Calend\303\241rio de Provas", 0, QApplication::UnicodeUTF8));
pshBReadOfDay->setText(QApplication::translate("MainWindow", "Leitura do Dia", 0, QApplication::UnicodeUTF8));
labelTitle->setText(QApplication::translate("MainWindow", "Escolha o que quer fazer hoje: ", 0, QApplication::UnicodeUTF8));
labelNews->setText(QApplication::translate("MainWindow", "Novidades:", 0, QApplication::UnicodeUTF8));
groupBox->setTitle(QApplication::translate("MainWindow", "Informa\303\247\303\265es", 0, QApplication::UnicodeUTF8));
labelCollege->setText(QApplication::translate("MainWindow", "Faculdade:", 0, QApplication::UnicodeUTF8));
labelCourse->setText(QApplication::translate("MainWindow", "Curso Pretendido:", 0, QApplication::UnicodeUTF8));
labelMemSince->setText(QApplication::translate("MainWindow", "Membro desde:", 0, QApplication::UnicodeUTF8));
labelLoggedWith->setText(QApplication::translate("MainWindow", "Voc\303\252 esta logado com: ", 0, QApplication::UnicodeUTF8));
labelBP->setText(QApplication::translate("MainWindow", "BP: ", 0, QApplication::UnicodeUTF8));
menuArquivo->setTitle(QApplication::translate("MainWindow", "Arquivo", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MAINWINDOW_H
You can use the signal and slot mechanism, for that please go through the following documentation..
http://qt-project.org/doc/qt-4.8/signalsandslots.html
Now you can connect the signal of one window to the another and in the slot of the other window you show the other window and hide the previous one.
well, after some patience and study of the Qt Architecture I did it.
I just added that line in Ui_MainWindow::SetupUI(blablabla)
studyWindow = new StudyWindow(MainWindow);
QObject::connect(pshBStudy, SIGNAL(clicked()), studyWindow, SLOT(show()));
And the respective attribute to Ui_MainWindow Class. And now it's working alright.
So you have one window A which "controls" the others W1, W2, ...., Wn?
One way is to do :
Associate each of the 3 buttons Bi to their respective window Wi (like a map where a key is a button and the value the QMainWindow)
Create a custom slot in window A and connect the clicked() signals of each of the buttons to this slot.
In this slot you find which button Bi sent the signal using sender(). You find the associated window Wi and you call show(). In the meanwhile you call hide() for all other windows Wj, j!=i
All the methods cited above are either in the doc of QWidget or Qobject, so you should read it.