True content is not copied from one QMap into another QMap - c++

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.

Related

Qt not removing item from layout

I have a simple Qt application that dynamically adds and deletes items from the layout, but it does not work, so I wrote the code below to illustrate what I'm doing:
The header file justs put an additional layout field in addition to Qt creator template:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFrame>
#include <QVBoxLayout>>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QFrame* create_frame();
void remove_frame();
QVBoxLayout* layout;
};
#endif // MAINWINDOW_H
The cpp file adds a few QFrame to the current layout, then clicking the button will remove them one by one. The QCoreApplication::processEvents and QThread::sleep just adds some animation to show what is happening.
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QPushButton>
#include <QThread>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
auto window = new QWidget(this);
setCentralWidget(window);
auto button = new QPushButton(window);
button->setGeometry(100, 100, 100, 100);
layout = new QVBoxLayout;
window->setLayout(layout);
for (int i = 0; i < 5; i++) {
auto x = create_frame();
layout->addWidget(x);
}
connect(button, &QPushButton::clicked, this, &MainWindow::remove_frame);
}
void MainWindow::remove_frame() {
while(layout->count() > 0) {
auto item = layout->itemAt(0);
layout->removeItem(item);
delete item;
QCoreApplication::processEvents();
QThread::msleep(200);
}
}
QFrame* MainWindow::create_frame() {
auto x = new QFrame();
x->setMinimumHeight(20);
x->setMaximumHeight(20);
x->setFrameStyle(QFrame::StyledPanel);
return x;
}
MainWindow::~MainWindow()
{
delete ui;
}
The animation is a mess, and does not completely remove all the QFrame boxes. What did I do wrong here? And later I find that if I use auto item = layout->itemAt(0)->widget(); layout->removeWidget(item); then I can indeed remove them, are removeItem and removeWidget doing different things?

How to set desired layout to the elements in different classes in c++ qt?

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.

QFileDialog How to set filename to the text field and use QFileDialog separately with few textfields

I'm trying to write GUI for a program that must perform some actions with given files. Its logic will be:
1) Program starts with one text field and one button created.
2) If I click on button, I can choose some .exe file. If the file is choosen, its path is set to the textfield that is logicaly linked with first my button.
3) If the file is choosen on previous step, a new pair of text field and button linked to it is created. The size of main window must change dynamically when a new pair is added.
How can I set the path to the file to current textfield?
I need a possibility to edit data in any text field. How to organize interface so that I could separately use QFileDialog with any pair of textfield and button.
I cant figure out how to use signals/slots in this case.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void makeInterface();
private slots:
void openFile();
};
#endif
#include <QString>
#include <QFileDialog>
#include <QDebug>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
makeInterface();
}
MainWindow::~MainWindow() {}
void MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(
this,
tr("OpenFile"),
QDir::currentPath(),
tr("Executable Files (*.exe)"));
if (!fileName.isNull())
{
qDebug() << fileName;
}
}
void MainWindow::makeInterface()
{
QGridLayout *mainLayout = new QGridLayout;
QLineEdit *fldFilePath = new QLineEdit;
QPushButton *btnOpenFile = new QPushButton("*.exe");
connect(btnOpenFile, SIGNAL(clicked()), this, SLOT(openFile()));
mainLayout->addWidget(fldFilePath, 0, 0);
mainLayout->addWidget(btnOpenFile, 0, 1);
QPushButton *btnBuild = new QPushButton("Build");
mainLayout->addWidget(btnBuild, 5, 0);
setLayout(mainLayout);
}
You should use QSignalMapper for this.
Your code may looks like this:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QSignalMapper>
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void makeInterface();
private slots:
void openFile(QWidget* widget);
private:
QSignalMapper _mapper;
};
#endif
#include <QString>
#include <QFileDialog>
#include <QDebug>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
connect(&_mapper, SIGNAL(mapped(QWidget*)), this, SLOT(openFile(QWidget*)));
makeInterface();
}
MainWindow::~MainWindow() {}
void MainWindow::openFile(QWidget* widget)
{
QString fileName = QFileDialog::getOpenFileName(
this,
tr("OpenFile"),
QDir::currentPath(),
tr("Executable Files (*.exe)"));
if (!fileName.isNull())
{
static_cast<QLineEdit*>(widget)->setText(fileName);
}
}
void MainWindow::makeInterface()
{
QGridLayout *mainLayout = new QGridLayout;
QLineEdit *fldFilePath = new QLineEdit;
QPushButton *btnOpenFile = new QPushButton("*.exe");
connect(btnOpenFile, SIGNAL(clicked()), &_mapper, SLOT(map()));
_mapper.setMapping(btnOpenFile, fldFilePath);
mainLayout->addWidget(fldFilePath, 0, 0);
mainLayout->addWidget(btnOpenFile, 0, 1);
QPushButton *btnBuild = new QPushButton("Build");
mainLayout->addWidget(btnBuild, 5, 0);
setLayout(mainLayout);
}

QT - connecting wrong button from member QWidget

I have a main window which opens a new window and connects a button from the new window to a "close" function. The problem arises when that new window has more than one button; it will always connect the last created button instead of the explicited one. Here is a sample working code:
"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.h"
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "screen_char_info.h"
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QPushButton *button_show_char_info;
Screen_Char_Info *screen_char_info;
QWidget *mainwidget;
QVBoxLayout *layout_main;
MainWindow(QWidget *parent = 0) : QMainWindow(parent) {
button_show_char_info = new QPushButton("Character info", this);
layout_main = new QVBoxLayout();
mainwidget = new QWidget(this);
screen_char_info = NULL;
QObject::connect (button_show_char_info, &QPushButton::clicked, [this]{
if (screen_char_info == NULL) {
screen_char_info = new Screen_Char_Info();
screen_char_info->show();
QObject::connect (screen_char_info->button_return, &QPushButton::clicked, [=] {
screen_char_info->close();
screen_char_info = NULL;
});
}
});
layout_main->addWidget(button_show_char_info);
mainwidget->setLayout(layout_main);
setCentralWidget(mainwidget);
}
~MainWindow()
{
}
};
#endif // MAINWINDOW_H
"screen_char_info.h"
#ifndef SCREEN_CHAR_INFO_H
#define SCREEN_CHAR_INFO_H
#include <QString>
#include <QMenu>
#include <QMenuBar>
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
#include <QWidget>
#include <QLineEdit>
#include <QGridLayout>
class Screen_Char_Info : public QWidget {
Q_OBJECT
public:
QPushButton *buttons_modify_attributes[15];
QPushButton *button_return;
QGridLayout *layout;
Screen_Char_Info () {
this->setAttribute(Qt::WA_DeleteOnClose);
this->setWindowTitle("Character Info");
layout = new QGridLayout(this);
for (int i = 0; i <= 15; i++) {
buttons_modify_attributes[i] = new QPushButton((i%2 ? "-" : "+"), this);
connect(buttons_modify_attributes[i], &QPushButton::clicked, [this] {
});
layout->addWidget(buttons_modify_attributes[i], (i / 2), (i % 2), 1, 1);
}
layout->addWidget(button_return = new QPushButton("Return", this), 8, 0, 1, 1);
this->setLayout(layout);
}
};
#endif // SCREEN_CHAR_INFO_H
However, if i put the line layout->addWidget(button_return... before the for loop, the button that closes the window is the last "-" button, and not the return one.
The way you do the connect does not appear to be conventional. Try using traditional Qt way:
connect(pButtonToPress, SIGNAL(clicked()), pObjectToHandle, SLOT(onClicked));
Provided QPushButton* pButtonPress actually pointing to QPushButton and pObjectToHandle to some object (can be 'this' pointer):
class ObjHandler
{
public slot:
void onClicked();
};
... should satisfy. SIGNAL and SLOT are macro that work with some help of Qt meta object compiler. That is why having slot: statement is highly critical.
Found the bug, I was declaring a button matrix with 15 elements, but iterating over a 16 element loop. The 16th element was the return button, and was overwritten in the loop.

Qt - QStackedWidget and QTabsExample help

Basically, I have multiple widgets I'm trying to switch between... and the default is a QTabWidget. Aside from some modification, the two examples (QStackedWidget and QTabsExapmle) are just mingled together. I can't get the "connect" portion to work (get an error: no matching function for call to QTabsExample::connect), and nothing displays on screen unless stackedWidget->addWidget(tabWidget) is the first in the list (and even then, I only see the view in the upper left hand corner).
QTabsExample.h
#ifndef QTABSEXAMPLE_H
#define QTABSEXAMPLE_H
#include <QtGui/QMainWindow>
#include <QtGui/QScrollArea>
#include <QtGui/QFrame>
#include <QtGui/QVBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QGroupBox>
#include <QtGui/QFormLayout>
#include <QtGui/QMessageBox>
#include <QtCore/QPointer>
#include <QtCore/QFile>
#include <QtCore/QIODevice>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtXml/QXmlStreamReader>
#include <QtDebug>
#include <QBool>
#include <QSignalMapper>
#include <QStackedLayout>
class QTabsExample: public QMainWindow {
Q_OBJECT
public:
QTabsExample(QWidget *parent = 0);
~QTabsExample();
private:
void buildTabMenuBar(int index);
public slots:
void activeTabChanged(int index);
void setCurrentIndex(int);
signals:
void activated(int);
private:
QTabWidget* tabWidget;
QWidget* customWidget1;
QWidget* customWidget2;
QWidget* customWidget3;
};
// ---------------------------------------------------------------------------
// QMyWidget - Tab1
//
class QMyWidget: public QWidget {
Q_OBJECT
public:
QMyWidget(QWidget *parent = 0);
~QMyWidget();
public slots:
void runOnTabSelect();
private:
QPointer<QVBoxLayout> _layout;
};
// ---------------------------------------------------------------------------
// QMyWidget2 - Tab2
//
class QMyWidget2: public QWidget {
Q_OBJECT
public:
QMyWidget2(QWidget *parent = 0);
~QMyWidget2();
public slots:
void runOnTabSelect();
};
// ---------------------------------------------------------------------------
// QMyWidget3 - Tab3
//
class QMyWidget3: public QWidget {
Q_OBJECT
public:
QMyWidget3(QWidget *parent = 0);
~QMyWidget3();
public slots:
void runOnTabSelect();
private:
QPointer<QVBoxLayout> _layout;
};
#endif // QTABSEXAMPLE_H
QTabsExample.cpp
#include <QtGui>
#include <QApplication>
#include "QTabsExample.h"
QTabsExample::QTabsExample(QWidget *parent) : QMainWindow(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
this->setWindowTitle("Main Window, I think");
QStackedLayout *stackedLayout = new QStackedLayout;
//create tab widget
QTabWidget *tabWidget = new QTabWidget();
tabWidget->setContextMenuPolicy(Qt::NoContextMenu);
QObject::connect(tabWidget, SIGNAL(currentChanged(int)),this, SLOT(activeTabChanged(int)));
QMyWidget* widget1 = new QMyWidget();
tabWidget->addTab(widget1, "Tab1");
QMyWidget2* widget2 = new QMyWidget2();
tabWidget->addTab(widget2, "Tab2");
QMyWidget3* widget3 = new QMyWidget3();
tabWidget->addTab(widget3, "Tab3");
//set programatically
tabWidget->setStyleSheet("QTabBar::tab { height: 70px; width: 80px; font-size: 15px;}");
//create other widgets
QWidget* customWidget1 = new QWidget;
QWidget* customWidget2 = new QWidget;
QWidget* customWidget3 = new QWidget;
//add layouts to widgets
customWidget1->setContextMenuPolicy(Qt::NoContextMenu);
customWidget2->setContextMenuPolicy(Qt::NoContextMenu);
customWidget3->setContextMenuPolicy(Qt::NoContextMenu);
customWidget1->setWindowTitle("Widget 1");
customWidget2->setWindowTitle("Widget 2");
customWidget3->setWindowTitle("Widget 3");
//insert content to make sure it's viewable
QPalette palette;
palette.setBrush(this->backgroundRole(), QBrush(QImage("c://default.png")));
customWidget1->setPalette(palette);
palette.setBrush(this->backgroundRole(), QBrush(QImage("c://default2.png")));
customWidget2->setPalette(palette);
palette.setBrush(this->backgroundRole(), QBrush(QImage("c://default3.png")));
customWidget3->setPalette(palette);
//add widgets to stack
stackedLayout->addWidget(tabWidget);
stackedLayout->addWidget(customWidget1);
stackedLayout->addWidget(customWidget2);
stackedLayout->addWidget(customWidget3);
QComboBox *pageComboBox = new QComboBox;
pageComboBox->addItem(tr("Tab Page"));
pageComboBox->addItem(tr("page 2"));
pageComboBox->addItem(tr("page 3"));
pageComboBox->addItem(tr("page 4"));
connect(pageComboBox, SIGNAL(activated(int))), stackedLayout, SLOT(setCurrentIndex(int));
QVBoxLayout *_layout = new QVBoxLayout;
_layout->addWidget(pageComboBox);
_layout->addLayout(stackedLayout);
setLayout(_layout);
//setCentralWidget(tabWidget);
#ifdef Q_OS_SYMBIAN
QWidgetList widgets = QApplication::allWidgets();
QWidget* w = 0;
foreach(w,widgets) {
w->setContextMenuPolicy(Qt::NoContextMenu);
}
#endif
}
QTabsExample::~QTabsExample(){
}
void QTabsExample::activeTabChanged(int index) {
buildTabMenuBar(index);
}
void QTabsExample::buildTabMenuBar(int index) {
QMenuBar* menubar = menuBar();
menubar->clear();
switch (index) {
case 0:
{
menubar->addAction("", tabWidget->widget(index), SLOT(runOnTabSelect()));
break;
}
case 1:
{
menubar->addAction("", tabWidget->widget(index), SLOT(runOnTabSelect()));
break;
}
case 2:
{
menubar->addAction("", tabWidget->widget(index), SLOT(runOnTabSelect()));
break;
}
default:
{
break;
}
};
}
//tab1
QMyWidget::QMyWidget(QWidget *parent) : QWidget(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
QVBoxLayout* _layout = new QVBoxLayout(this);
//buttons get created
this->setLayout(_layout);
}
QMyWidget::~QMyWidget() {
}
void QMyWidget::runOnTabSelect() {
}
//tab2
QMyWidget2::QMyWidget2(QWidget *parent) : QWidget(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
QVBoxLayout* _layout = new QVBoxLayout(this);
//buttons get created
this->setLayout(_layout);
}
QMyWidget2::~QMyWidget2() {
}
void QMyWidget2::runOnTabSelect() {
}
//tab3
QMyWidget3::QMyWidget3(QWidget *parent) : QWidget(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
QVBoxLayout* _layout = new QVBoxLayout(this);
//buttons get created
this->setLayout(_layout);
}
QMyWidget3::~QMyWidget3() {
}
void QMyWidget3::runOnTabSelect() {
}
main.cpp
#include "QTabsExample.h""
#include <QtGui>
#include <QApplication>
#include <QtGui/QApplication>
#include <QPixmap>
#include <QWidget>
#include <QMainWindow>
#include <QSplashScreen>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTabsExample w;
w.showMaximized();
return a.exec();
}
Well for starters you need to uncomment //QTabsExample w; in main.cpp, but I guess, that is just a relict from experimenting...
Also, what do you mean by "nothing displays on screen."?
Do you mean, that an empty window pops up? Or does no window at all open? Or does the screen turn black?
Have you eliminated the possibility that the "app.css" file might be corrupt?
Have you tried deleting the Makefiles and moc_* files?
EDIT:
I tried to simplify the constructor of QTabsExample
#include <QtGui>
#include <QApplication>
#include "QTabsExample.h"
QTabsExample::QTabsExample(QWidget *parent) : QMainWindow(parent) {
setContextMenuPolicy(Qt::NoContextMenu);
this->setWindowTitle("Main Window, I think");
//create tab widget
QTabWidget *tabWidget = new QTabWidget();
tabWidget->setContextMenuPolicy(Qt::NoContextMenu);
//QObject::connect(tabWidget, SIGNAL(currentChanged(int)),this, SLOT(activeTabChanged(int))); // I have no experience with mobile developement
// can you manipulate the menubar on a mobile device?
QMyWidget* widget1 = new QLabel(tr("Widget1")); // simplification
tabWidget->addTab(widget1, "Tab1");
QMyWidget2* widget2 = new QLabel(tr("Widget2"));
tabWidget->addTab(widget2, "Tab2");
QMyWidget3* widget3 = new QLabel(tr("Widget3"));
tabWidget->addTab(widget3, "Tab3");
//set programatically
tabWidget->setStyleSheet("QTabBar::tab { height: 70px; width: 80px; font-size: 15px;}");
//create other widgets
QWidget* customWidget1 = new QLabel(tr("Hello1")); // simplification
QWidget* customWidget2 = new QLabel(tr("Hello2"));
QWidget* customWidget3 = new QLabel(tr("Hello3"));
// create stacked layout (closer to where it is actually used)
QStackedLayout *stackedLayout = new QStackedLayout;
//add widgets to stack
stackedLayout->addWidget(tabWidget);
stackedLayout->addWidget(customWidget1);
stackedLayout->addWidget(customWidget2);
stackedLayout->addWidget(customWidget3);
QComboBox *pageComboBox = new QComboBox;
pageComboBox->addItem(tr("Tab Page"));
pageComboBox->addItem(tr("page 2"));
pageComboBox->addItem(tr("page 3"));
pageComboBox->addItem(tr("page 4"));
connect(pageComboBox, SIGNAL(activated(int))), stackedLayout, SLOT(setCurrentIndex(int));
QVBoxLayout *_layout = new QVBoxLayout;
_layout->addWidget(pageComboBox);
_layout->addLayout(stackedLayout);
setLayout(_layout);
//setCentralWidget(tabWidget);
#ifdef Q_OS_SYMBIAN
QWidgetList widgets = QApplication::allWidgets();
QWidget* w = 0;
foreach(w,widgets) {
w->setContextMenuPolicy(Qt::NoContextMenu);
}
#endif
}
Expected behaviour:
a combo box on the top with choices: "Tab Page", "page 2", ...
below a QTabWidget witht the tabs: "Tab1", "Tab2", "Tab3"
Tab1 should be displayed with a QLabel, which says "Widget1"
when you select another tab, the label should say "WidgetX" // depending on your choice
when you select another widget from the combo box you should see "HelloX" // depending on your choice