Related
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.
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/
I have a QScrollArea with some buttons in it, like shown on the picture.
The idea of the layout is:
1. The left and right button should be used for scrolling the buttons when they are too wide
2.The numbers of buttons in the scroll area can be changed dynamically
3. Any free space should be used to expand the scroll area as much as possible. If no such space exist navigation buttons should be used for scrolling.
With my current implementation when i increase the buttons i have this:
But there is free space on the right, so this should look like:
If i increase once more to 10 for example, then scrollbar should appear( because the layout is constained by the widget ).
I want to know if there is any other way aside from manual resizing of the widgets( because ui can be translated and buttons can change size hint also the real design is more complicated :(
Here is my implementation of the ScrollAreaTest widget:
#include "MainWidget.h"
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QScrollArea>
#include <QPushButton>
#include <QDebug>
#include "ButtonWidget.h"
#include "CheckableButtonGroup.h"
MainWidget::MainWidget(QWidget *parent)
: QWidget(parent),
m_scrollArea( 0 ),
m_lineEdit( 0 ),
m_buttons( 0 )
{
QVBoxLayout* mainLayout = new QVBoxLayout( this );
QWidget* firstRow = new QWidget;
QHBoxLayout* firstRowLayout = new QHBoxLayout( firstRow );
QPushButton* left = new QPushButton;
QPushButton* right = new QPushButton;
m_buttons = new CheckableButtonGroup( Qt::Horizontal );
m_buttons->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
m_buttons->setButtonsCount( 5 );
m_buttons->setStyleSheet( "border: none" );
QWidget* const buttonsContainer = new QWidget;
QHBoxLayout* const buttonsContainerLayout = new QHBoxLayout( buttonsContainer );
buttonsContainerLayout->setSpacing( 0 );
buttonsContainerLayout->setSizeConstraint( QLayout::SetMinAndMaxSize );
buttonsContainerLayout->setMargin( 0 );
buttonsContainerLayout->addWidget( m_buttons, 0, Qt::AlignLeft );
qDebug() << m_buttons->buttons()[ 0 ]->size();
m_scrollArea = new QScrollArea;
m_scrollArea->setContentsMargins( 0, 0, 0, 0 );
m_scrollArea->setWidget( buttonsContainer );
m_scrollArea->setWidgetResizable( true );
m_scrollArea->setStyleSheet( "border: 1px solid blue" );
m_scrollArea->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
firstRowLayout->addWidget( left , 0, Qt::AlignLeft );
firstRowLayout->addWidget( m_scrollArea, 1, Qt::AlignLeft );
firstRowLayout->addWidget( right , 0, Qt::AlignLeft );
m_lineEdit = new QLineEdit;
QPushButton* button = new QPushButton;
QHBoxLayout* secondRowLayout = new QHBoxLayout;
secondRowLayout->addWidget( m_lineEdit );
secondRowLayout->addWidget( button );
connect( button, SIGNAL(clicked()), SLOT(setButtonsCount()) );
mainLayout->addWidget( firstRow, 1, Qt::AlignLeft );
mainLayout->addLayout( secondRowLayout );
button->setText( "Set buttons count" );
buttonsContainer->resize( m_buttons->buttonsOptimalWidth(), buttonsContainer->height() );
m_buttons->resize( m_buttons->buttonsOptimalWidth(), m_buttons->height() );
//area->resize( 100, area->height() );
//area->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
}
MainWidget::~MainWidget()
{
}
void MainWidget::setButtonsCount()
{
m_buttons->setButtonsCount( m_lineEdit->text().toInt() );
}
And here is the whole Qt project containing the problem:
https://drive.google.com/file/d/0B-mc4aKkzWlxQzlPMEVuNVNKQjg/edit?usp=sharing
The essential steps are:
The container widget that holds the buttons (your CheckableButtonGroup) must have a QLayout::SetMinAndMaxSize size constraint set. Then it will be exactly large enough to hold the buttons. Its size policy doesn't matter, since you're simply putting it into a QScrollArea, not into another layout.
The scroll area needs to set its maximum size according to the size of the widget it holds. The default implementation doesn't do it, so one has to implement it by spying on resize events of the embedded widget.
The code below is a minimal example that works under both Qt 4.8 and 5.2.
// https://github.com/KubaO/stackoverflown/tree/master/questions/scrollgrow-21253755
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif
class ButtonGroup : public QWidget {
Q_OBJECT
QHBoxLayout m_layout{this};
public:
ButtonGroup(QWidget * parent = 0) : QWidget{parent} {
m_layout.setSizeConstraint(QLayout::SetMinAndMaxSize); // <<< Essential
}
Q_SLOT void addButton() {
auto n = m_layout.count();
m_layout.addWidget(new QPushButton{QString{"Btn #%1"}.arg(n+1)});
}
};
class AdjustingScrollArea : public QScrollArea {
bool eventFilter(QObject * obj, QEvent * ev) {
if (obj == widget() && ev->type() == QEvent::Resize) {
// Essential vvv
setMaximumWidth(width() - viewport()->width() + widget()->width());
}
return QScrollArea::eventFilter(obj, ev);
}
public:
AdjustingScrollArea(QWidget * parent = 0) : QScrollArea{parent} {}
void setWidget(QWidget *w) {
QScrollArea::setWidget(w);
// It happens that QScrollArea already filters widget events,
// but that's an implementation detail that we shouldn't rely on.
w->installEventFilter(this);
}
};
class Window : public QWidget {
QGridLayout m_layout{this};
QLabel m_left{">>"};
AdjustingScrollArea m_area;
QLabel m_right{"<<"};
QPushButton m_add{"Add a widget"};
ButtonGroup m_group;
public:
Window() {
m_layout.addWidget(&m_left, 0, 0);
m_left.setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
m_left.setStyleSheet("border: 1px solid green");
m_layout.addWidget(&m_area, 0, 1);
m_area.setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
m_area.setStyleSheet("QScrollArea { border: 1px solid blue }");
m_area.setWidget(&m_group);
m_layout.setColumnStretch(1, 1);
m_layout.addWidget(&m_right, 0, 2);
m_right.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
m_right.setStyleSheet("border: 1px solid green");
m_layout.addWidget(&m_add, 1, 0, 1, 3);
connect(&m_add, SIGNAL(clicked()), &m_group, SLOT(addButton()));
}
};
int main(int argc, char *argv[])
{
QApplication a{argc, argv};
Window w;
w.show();
return a.exec();
}
#include "main.moc"
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.
I have made a user interface in Qt Designer and i now want to display the data.I want to make use of the multiple inheritance approach.Here is the code for the user interface.
/********************************************************************************
** Form generated from reading UI file 'prototypejI3444.ui'
**
** Created: Mon May 30 10:04:01 2011
** by: Qt User Interface Compiler version 4.7.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef PROTOTYPEJI3444_H
#define PROTOTYPEJI3444_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QGridLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QTableView>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public:
QWidget *centralwidget;
QGridLayout *gridLayout;
QVBoxLayout *verticalLayout_2;
QLabel *label;
QVBoxLayout *verticalLayout_3;
QLineEdit *lineEdit;
QVBoxLayout *verticalLayout_4;
QLabel *label_2;
QVBoxLayout *verticalLayout_5;
QLineEdit *lineEdit_2;
QVBoxLayout *verticalLayout_6;
QLabel *label_3;
QVBoxLayout *verticalLayout_7;
QLineEdit *lineEdit_3;
QTableView *tableView;
QPushButton *pushButton_2;
QPushButton *pushButton_3;
QPushButton *pushButton_4;
QPushButton *pushButton_5;
QPushButton *pushButton_6;
QPushButton *pushButton;
QMenuBar *menubar;
QStatusBar *statusbar;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(800, 600);
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
gridLayout = new QGridLayout(centralwidget);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
verticalLayout_2 = new QVBoxLayout();
verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
label = new QLabel(centralwidget);
label->setObjectName(QString::fromUtf8("label"));
verticalLayout_2->addWidget(label);
gridLayout->addLayout(verticalLayout_2, 1, 0, 1, 1);
verticalLayout_3 = new QVBoxLayout();
verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3"));
lineEdit = new QLineEdit(centralwidget);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
verticalLayout_3->addWidget(lineEdit);
gridLayout->addLayout(verticalLayout_3, 1, 1, 1, 1);
verticalLayout_4 = new QVBoxLayout();
verticalLayout_4->setObjectName(QString::fromUtf8("verticalLayout_4"));
label_2 = new QLabel(centralwidget);
label_2->setObjectName(QString::fromUtf8("label_2"));
verticalLayout_4->addWidget(label_2);
gridLayout->addLayout(verticalLayout_4, 2, 0, 1, 1);
verticalLayout_5 = new QVBoxLayout();
verticalLayout_5->setObjectName(QString::fromUtf8("verticalLayout_5"));
lineEdit_2 = new QLineEdit(centralwidget);
lineEdit_2->setObjectName(QString::fromUtf8("lineEdit_2"));
verticalLayout_5->addWidget(lineEdit_2);
gridLayout->addLayout(verticalLayout_5, 2, 1, 1, 1);
verticalLayout_6 = new QVBoxLayout();
verticalLayout_6->setObjectName(QString::fromUtf8("verticalLayout_6"));
label_3 = new QLabel(centralwidget);
label_3->setObjectName(QString::fromUtf8("label_3"));
verticalLayout_6->addWidget(label_3);
gridLayout->addLayout(verticalLayout_6, 3, 0, 1, 1);
verticalLayout_7 = new QVBoxLayout();
verticalLayout_7->setObjectName(QString::fromUtf8("verticalLayout_7"));
lineEdit_3 = new QLineEdit(centralwidget);
lineEdit_3->setObjectName(QString::fromUtf8("lineEdit_3"));
verticalLayout_7->addWidget(lineEdit_3);
gridLayout->addLayout(verticalLayout_7, 3, 1, 1, 1);
tableView = new QTableView(centralwidget);
tableView->setObjectName(QString::fromUtf8("tableView"));
gridLayout->addWidget(tableView, 0, 0, 1, 2);
pushButton_2 = new QPushButton(centralwidget);
pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
gridLayout->addWidget(pushButton_2, 2, 2, 1, 1);
pushButton_3 = new QPushButton(centralwidget);
pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
gridLayout->addWidget(pushButton_3, 3, 2, 1, 1);
pushButton_4 = new QPushButton(centralwidget);
pushButton_4->setObjectName(QString::fromUtf8("pushButton_4"));
gridLayout->addWidget(pushButton_4, 1, 3, 1, 1);
pushButton_5 = new QPushButton(centralwidget);
pushButton_5->setObjectName(QString::fromUtf8("pushButton_5"));
gridLayout->addWidget(pushButton_5, 2, 3, 1, 1);
pushButton_6 = new QPushButton(centralwidget);
pushButton_6->setObjectName(QString::fromUtf8("pushButton_6"));
gridLayout->addWidget(pushButton_6, 3, 3, 1, 1);
pushButton = new QPushButton(centralwidget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
gridLayout->addWidget(pushButton, 1, 2, 1, 1);
MainWindow->setCentralWidget(centralwidget);
tableView->raise();
pushButton_2->raise();
pushButton_3->raise();
pushButton_4->raise();
pushButton_5->raise();
pushButton_6->raise();
pushButton->raise();
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 800, 18));
MainWindow->setMenuBar(menubar);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
label->setText(QApplication::translate("MainWindow", "FirstName:", 0, QApplication::UnicodeUTF8));
label_2->setText(QApplication::translate("MainWindow", "SecondName:", 0, QApplication::UnicodeUTF8));
label_3->setText(QApplication::translate("MainWindow", "City:", 0, QApplication::UnicodeUTF8));
pushButton_2->setText(QApplication::translate("MainWindow", "New", 0, QApplication::UnicodeUTF8));
pushButton_3->setText(QApplication::translate("MainWindow", "Delete", 0, QApplication::UnicodeUTF8));
pushButton_4->setText(QApplication::translate("MainWindow", "Next", 0, QApplication::UnicodeUTF8));
pushButton_5->setText(QApplication::translate("MainWindow", "Previous", 0, QApplication::UnicodeUTF8));
pushButton_6->setText(QApplication::translate("MainWindow", "First", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("MainWindow", "Save", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // PROTOTYPEJI3444_H
In the multiple inheritance method there is a .h file and its corresponding .cpp.I want someone to guide me on where to make a connection to sqlite database(whether its in the .cpp file or the .h file) and how to communicate with the UI file i have shown to show data from the database on Qtableview.
Thanks.
You instantiate QSqlTableModel, associate it the the view using setModel, and initialize the appropriate table properties to the model. Do all of it in init.