I'm currently learning Qt by making a sort of "RecipeBook" application. It has a function that currently should only add layouts from QMap to my main QLayout. Unfortunately, said error occurs:
It happens most of the times when adding the last layout, but it's not a rule. I experimented much with this code, so I'm sorry it's so messy. When I started RefillRecipeLayout function the statement was "while(i != endIterator), but it behaved the same. Below I'm sending you my code:
mainwindow.cpp
void MainWindow::on_recalculateButton_clicked()
{
currentRecipe.RefreshIngredientsInMap(deleteButtonToIngredientLayoutMap);
RefillRecipeLayout();
}
void MainWindow::DeleteLayout(QLayout* layout)
{
ClearLayout(layout);
delete layout;
}
void MainWindow::ClearLayout(QLayout* layout)
{
while (layout->count() != 0)
{
QLayoutItem* item = layout->takeAt(0);
delete item->widget();
delete item;
}
}
void MainWindow::RefreshRecipeLayout()
{
ClearLayout(recipeLayout);
}
void MainWindow::RefillRecipeLayout()
{
ClearLayout(recipeLayout);
int recipeCount= recipeLayout->count();
int iCount=0;
QMap<QPushButton*, QHBoxLayout*>::const_iterator i = deleteButtonToIngredientLayoutMap.constBegin();
QMap<QPushButton*, QHBoxLayout*>::const_iterator endIterator = deleteButtonToIngredientLayoutMap.constEnd();
int end = std::distance(i, endIterator);
while (iCount < end)
{
QLayout* layout = i.value();
int recipeCount = recipeLayout->count();
auto isEmpty=recipeLayout->isEmpty();
auto isEnabled = recipeLayout->isEnabled();
bool exists = recipeLayout;
recipeLayout->addLayout(layout);
i++;
iCount++;
}
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QComboBox>
#include <QSpinBox>
#include <QTextEdit>
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QScrollArea>
#include <QScrollBar>
#include "recipe.h"
#include "ingredient.h"
#include <vector>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_addIngredient_clicked();
void on_ExitButton_clicked();
void on_recalculateButton_clicked();
private:
Ui::MainWindow *ui;
QMap<QPushButton*, QHBoxLayout*> deleteButtonToIngredientLayoutMap;
QComboBox* ingredientUnitComboBox=nullptr;
QSpinBox* ingredientAmountSpinBox=nullptr;
QTextEdit* ingredientNameText=nullptr;
QLabel* recipeAmountLabel=nullptr;
QLabel* recipeNameLabel=nullptr;
QComboBox* recipeUnitCBox=nullptr;
QComboBox* newRecipeUnitCBox=nullptr;
QScrollArea* scrollArea=nullptr;
QVBoxLayout* recipeLayout=nullptr;
QSpinBox* recalculateSpinBox=nullptr;
QComboBox* recalculateUnitCBox=nullptr;
void PrepareComboBox(QComboBox* comboBox);
void DeleteIngredient();
void DeleteLayout(QLayout* layout);
void ClearLayout(QLayout* layout);
void RefillRecipeLayout();
Unit GetUnitFromCBox(QComboBox* comboBox);
void RefreshRecipeLayout();
};
#endif // MAINWINDOW_H
RefreshIngredientsFunction from Recipe.cpp (works well as far as I know, I've checked with breakpoints)
void Recipe::RefreshIngredientsInMap(QMap<QPushButton*, QHBoxLayout*> OtherMap)
{
QMap<QPushButton*, QHBoxLayout*>::const_iterator i = OtherMap.constBegin();
QMap<QPushButton*, QHBoxLayout*>::const_iterator iteratorEnd = OtherMap.constEnd();
int count=0;
while (i != iteratorEnd)
{
QHBoxLayout* ingredientLayout=new QHBoxLayout();
QHBoxLayout* layout = i.value();
int end = std::distance(OtherMap.constBegin(), OtherMap.constEnd());
int currentItemIndex=0;
while(layout->count()>0)
{
QWidget* widget = layout->takeAt(0)->widget();
if (QSpinBox* spinBox = qobject_cast<QSpinBox*>(widget))
spinBox->setValue(13);
ingredientLayout->addWidget(widget);
currentItemIndex++;
}
delete layout;
OtherMap.insert(i.key(), ingredientLayout);
i++;
count++;
}
}
Apart from some code style issues (irrelevant debug lines, don't name iterator "i") in your
void Recipe::RefreshIngredientsInMap(QMap<QPushButton*, QHBoxLayout*> OtherMap)
you are taking the map by copy. Then you are trying to modify it's content by deleting some layouts and inserting other layouts.
delete layout;
OtherMap.insert(i.key(), ingredientLayout);
The delete operation destroys the object your map was pointing to, and then after RefreshIngredientsInMap returns, the original map is pointing to destroyed objects, because the insertions were made on a copy.
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 months ago.
I am working on a project that utilizes a whole suite of custom widget that my company uses.
I am trying to subclass the QLineEdit QWidget so that I can override the default arrow key behavior. It should just behave like a regular QLineEdit.
I then want to use this custom QLineEdit (that I named DataLineEdit), in place of the standard QLineEdits that I dynamically created in another Widget, DataWidget.
I have followed tutorial after tutorial on subclassing, but cannot avoid the error:
undefined reference to "DataLineEdit::DataLineEdit(QWidget*)
My code is below.
datalineedit.h
#ifndef DATALINEEDIT_H
#define DATALINEEDIT_H
#include <QLineEdit>
#include <QWidget>
class DataLineEdit: public QLineEdit
{
Q_OBJECT
public:
explicit DataLineEdit(QWidget *parent = 0);
~DataLineEdit();
protected:
void keyPressEvent(QKeyEvent* event);
signals:
void leftRightOverrideKeyPress(int state);
private:
};
#endif // CUSTOMLINEEDIT_H
datalineedit.cpp
#include "datalineedit.h"
#include<QKeyEvent>
DataLineEdit::DataLineEdit(QWidget*parent) : QLineEdit(parent)
{
}
DataLineEdit::~DataLineEdit()
{
}
void DataLineEdit::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Left && this->cursorPosition() == 0)
{
emit leftRightOverrideKeyPress(0);
}
else if(event->key() == Qt::Key_Right && this->cursorPosition() == 2)
{
emit leftRightOverrideKeyPress(1);
}
else
{
QLineEdit::keyPressEvent(event);
}
}
datawidget.h
#define DATAWIDGET_H
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>
#include <QLineEdit>
#include <QObject>
#include <QtCore>
#include <QtGui>
#include <QKeyEvent>
#include <QMainWindow>
#include "datalineedit.h"
extern int focusRow;
extern int focusCol;
extern int numRows;
extern int numBottomRowCols;
extern QLineEdit *focusedLineEdit;
namespace Ui{
class DataWidget;
}
class QLineEdit;
class QLabel;
class DataWidget : public QWidget
{
Q_OBJECT
public:
explicit DataWidget(QWidget *parent = 0);
~DataWidget();
QString data;
protected:
bool eventFilter(QObject *object, QEvent *event);
public slots:
void focusChanged(QWidget *old, QWidget *now);
void arrowKeyNavigation(int state);
void on_applyChangePushButton_clicked();
void setData(QString text);
QString getData();
private slots:
void updateDataGridVisibility();
void on_payloadLineEdit_textChanged(const QString &arg1);
void on_savePushButton_clicked();
void on_openPushButton_clicked();
void on_presetComboBox_currentIndexChanged(int index);
private:
Ui::DataWidget *ui;
QString currentFile = "";
};
#endif // DATAWIDGET_H
datawidget.cpp
#include "datawidget.h"
#include "datawidget.h"
#include "datalineedit.h"
#include <QGridLayout>
#include <QLineEdit>
#include <QGroupBox>
#include <QKeyEvent>
#include <QIntValidator>
int focusRow;
int focusCol;
int numRows;
int numBottomRowCols;
QLineEdit *focusedLineEdit;
enum key_directions
{
arrowLeft = 0,
arrowRight = 1,
arrowUp = 2,
arrowDown = 3
};
enum presets
{
preset1 = 0,
preset2 = 1,
preset3 = 2,
preset4 = 3,
preset5 = 4,
clean = 5,
zeroes = 6
};
DataWidget::DataWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::DataWidget)
{
ui->setupUi(this);
this->installEventFilter(this);
connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(focusChanged(QWidget*,QWidget*)));
//payloadLineEdit
ui->payloadLineEdit->setValidator(new QIntValidator(0,64, this));
ui->payloadLineEdit->setFixedSize(20,20);
ui->payloadLineEdit->setTextMargins(0,0,0,0);
//dataGridLayout
ui->dataGridLayout->setAlignment(Qt::AlignLeft);
ui->dataGridLayout->setAlignment(Qt::AlignTop);
for(int i=0; i<4; i++)
{
for(int j=0; j<16; j++)
{
DataLineEdit *lineEdit = new DataLineEdit();
lineEdit->setText("00");//set data default
lineEdit->setInputMask("Hh");//limit to hex values
lineEdit->setFixedSize(20,20);
lineEdit->setTextMargins(0,0,0,0);
lineEdit->setAlignment(Qt::AlignCenter);
lineEdit->setAlignment(Qt::AlignCenter);
connect(lineEdit, SIGNAL(leftRightOverrideKeyPress(int)), this, SLOT(arrowKeyNavigation(int)));
ui->dataGridLayout->addWidget(lineEdit, i, j);
}
}
updateDataGridVisibility();
}
DataWidget::~DataWidget()
{
delete ui;
}
void DataWidget::arrowKeyNavigation(int state)
{
//does something
}
You could edit the class in the following form:
class DataLineEdit: public QLineEdit
{
Q_OBJECT
public:
explicit DataLineEdit(QWidget* parent = 0);
...
and
DataLineEdit::DataLineEdit(QWidget* parent) : QLineEdit(parent)
{
}
Thank you for all the help, it was definitely a linker issue but since I am working within a much larger project, I didn't want to mess with imports or .pro files too much. DataLineEdit is specific to DataWidget, so I simply moved the entire datalineedit.h file into the datawidget.h file at the bottom, and the same for the .cpp files. This avoids any linking all together and keeps things tidier based on the project's file hierarchy.
I'm trying to set up a QFormLayout so that some buttons show or hide the part below them, and using setHidden on the widgets I want to hide is resulting in a bad layout due to the form padding still showing.
So I tried this:
#include <QApplication>
#include <QCheckBox>
#include <QLabel>
#include <QFormLayout>
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
QCheckBox checkA;
QFormLayout * m_formLayout;
QWidget * m_widget;
public:
explicit MainWindow(QWidget *parent = 0) :
QMainWindow(parent),
m_formLayout(0L),
m_widget(0L)
{
populate();
connect(&checkA, &QCheckBox::stateChanged, this, &MainWindow::populate);
}
virtual ~MainWindow() { clear(); }
bool doesOwnObject(void * it) const
{
return (uintptr_t)this <= (uintptr_t)it && (uintptr_t)it < (uintptr_t)(this+1);
}
void clear()
{
if(m_formLayout)
{
QLayoutItem *child;
while ((child = m_formLayout->takeAt(0)) != 0)
{
QLayout * layout = child->layout();
QSpacerItem * spacer = child->spacerItem();
QWidget * widget = child->widget();
if(layout && !doesOwnObject(layout)) delete layout;
if(spacer && !doesOwnObject(spacer)) delete spacer;
if(widget && !doesOwnObject(widget)) delete widget;
}
delete m_formLayout;
m_formLayout = 0L;
}
}
void populate()
{
if(m_widget) { clear(); return; }
m_widget = new QWidget(this);
setCentralWidget(m_widget);
m_formLayout = new QFormLayout(m_widget);
m_formLayout->addRow(tr("Show Check Box B:"), &checkA);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
But that doesn't seem to work as clear() doesn't seem to be actually depopulating the form layout. The widgets remain where they were before the QFormLayout was deleted. And if m_widget is deleted then the program will crash as it tries to delete checkA, because checkA has not been removed from the now deleted form.
How do I fix this?
I have come to a solution that has the desirable (or what I understood) behavior. However, I've moved everything to pointers (since is what I am used to with Qt).
Header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QCheckBox>
#include <QLayoutItem>
#include <QFormLayout>
#include <QMainWindow>
#include <QLabel>
class MainWindow : public QMainWindow
{
Q_OBJECT
QCheckBox *checkA, *checkB;
QLabel *labelA, *labelB, *labelC;
QFormLayout * m_formLayout;
public:
explicit MainWindow(QWidget *parent = 0);
virtual ~MainWindow();
void clear();
public slots:
void populate();
};
#endif // MAINWINDOW_H
cpp file:
#include "mainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget * centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
m_formLayout = new QFormLayout(centralWidget);
checkA = new QCheckBox;
labelA = new QLabel("Show Check Box B:");
connect(checkA, &QCheckBox::stateChanged, this, &MainWindow::populate);
int row = 0;
m_formLayout->insertRow(row++, labelA, checkA);
checkB = new QCheckBox;
labelB = new QLabel("Check Box B:");
m_formLayout->insertRow(row++, labelB, checkB);
labelC = new QLabel("Bottom label");
m_formLayout->insertRow(row++,labelC);
populate();
}
MainWindow::~MainWindow() {
}
void MainWindow::clear()
{
if (!checkA->isChecked())
{
checkB->hide();
labelB->hide();
}
}
void MainWindow::populate()
{
clear();
if(checkA->isChecked())
{
checkB->show();
labelB->show();
}
}
Solution:
void clear()
{
QLayoutItem *child;
while ((child = m_formLayout->takeAt(0)) != 0)
{
QLayout * layout = child->layout();
QSpacerItem * spacer = child->spacerItem();
QWidget * widget = child->widget();
if(layout && !doesOwnObject(layout)) delete layout;
if(spacer && !doesOwnObject(spacer)) delete spacer;
if(widget)
{
if(doesOwnObject(widget)) widget->setParent(0L);
else delete widget;
}
}
}
setParent had to be called.
I'm trying to create a program that accepts images through drag and drop and shows those images on my UI, then I want to rename them and save them.
I got the drag and drop to work, but I have some issues with my Image placement and I can't seem to find where I'm making my mistake.
In the image you see my UI during runtime, in the top left you can see a part of the image I dragged into the green zone(this is my drag and drop zone that accepts images). The position I actually want it to be in should be the red square. The green zone is a Dynamic created object called Imagehandler that I created to handle the drag and drop of the images. The Red square is my own class that inherits from QLabel, I called it myiconclass. This class should hold the actual image data.
I think my mistake has to do with the layouts, but I can't see it.
Could I get some help with this please?
Imagehandler.h
#ifndef IMAGEHANDLER_H
#define IMAGEHANDLER_H
#include <QObject>
#include <QWidget>
#include <QLabel>
#include <QDrag>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QList>
#include <QDebug>
//this class is designed to help me take in the images with drag and drop
class ImageHandler : public QWidget
{
Q_OBJECT
public:
explicit ImageHandler(QWidget *parent = nullptr);
QList<QImage> getImageListMemory() const;
void setImageListMemory(const QList<QImage> &value);
QList<QUrl> getUrlsMemory() const;
void setUrlsMemory(const QList<QUrl> &value);
private:
//QWidget Icon;
QLabel Icon;
QList <QImage> imageListMemory;
QList <QUrl> urlsMemory;
protected:
void dragEnterEvent(QDragEnterEvent * event);
void dragLeaveEvent(QDragLeaveEvent * event);
void dragMoveEvent(QDragMoveEvent * event);
void dropEvent(QDropEvent * event);
signals:
void transferImageSignal(QList <QImage>);
public slots:
};
#endif // IMAGEHANDLER_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QImageReader>
#include <QList>
#include <QWidget>
#include <QLabel>
#include <myiconclass.h>
#include <imagehandler.h>
#include <QGridLayout>
#include <QDebug>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QList<QImage> getImageListMemory() const;
void setImageListMemory(const QList<QImage> &value);
private:
Ui::MainWindow *ui;
QLabel Icon;
QList <QImage> imageListMemory;
QList <QUrl> urlsMemory;
QList<QWidget *> labelList;
ImageHandler * ImageHandlerMemory;
QGridLayout * grid2;
QList <MyIconClass *> memory;
signals:
public slots:
void setIconSlot(QList <QImage>);
};
#endif // MAINWINDOW_H
myiconclass.h
#ifndef MYICONCLASS_H
#define MYICONCLASS_H
#include <QWidget>
#include <QLabel>
//this class is based on a Qlabel and is only made so it can help me with the actual images, gives me more members if I need it
class MyIconClass : public QLabel
{
Q_OBJECT
public:
explicit MyIconClass(QWidget *parent = nullptr);
int getMyNumber() const;
void setMyNumber(int value);
private:
int myNumber;
signals:
public slots:
};
#endif // MYICONCLASS_H
imagehandler.cpp
#include "imagehandler.h"
ImageHandler::ImageHandler(QWidget *parent) : QWidget(parent)
{
setAcceptDrops(true);
}
QList<QImage> ImageHandler::getImageListMemory() const
{
return imageListMemory;
}
void ImageHandler::setImageListMemory(const QList<QImage> &value)
{
imageListMemory = value;
}
QList<QUrl> ImageHandler::getUrlsMemory() const
{
return urlsMemory;
}
void ImageHandler::setUrlsMemory(const QList<QUrl> &value)
{
urlsMemory = value;
}
void ImageHandler::dragEnterEvent(QDragEnterEvent * event)
{
event->accept();
}
void ImageHandler::dragLeaveEvent(QDragLeaveEvent * event)
{
event->accept();
}
void ImageHandler::dragMoveEvent(QDragMoveEvent * event)
{
event->accept();
}
void ImageHandler::dropEvent(QDropEvent * event)
{
QList <QImage> imageList2;
QList <QUrl> urls;
QList <QUrl>::iterator i;
urls = event->mimeData()->urls();
//imageList.append(event->mimeData()->imageData());
foreach (const QUrl &url, event->mimeData()->urls())
{
QString fileName = url.toLocalFile();
qDebug() << "Dropped file:" << fileName;
qDebug()<<url.toString();
QImage img;
if(img.load(fileName))
{
imageList2.append(img);
}
}
emit transferImageSignal(imageList2);
this->setUrlsMemory(urls);
this->setImageListMemory(imageList2);
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ImageHandler * handler = new ImageHandler(this);
handler->show();
QGridLayout *grid = new QGridLayout;
grid->addWidget(handler, 0, 0);
ui->groupBoxIcon->setLayout(grid);
ImageHandlerMemory = handler;
//connect(handler,SIGNAL(handler->transferImageSignal(QList <QUrl>)),this,SLOT(setIconSlot(QList <QUrl>)));
connect(handler,SIGNAL(transferImageSignal(QList<QImage>)),this,SLOT(setIconSlot(QList<QImage>)));
}
MainWindow::~MainWindow()
{
delete ui;
}
QList<QImage> MainWindow::getImageListMemory() const
{
return imageListMemory;
}
void MainWindow::setImageListMemory(const QList<QImage> &value)
{
imageListMemory = value;
}
void MainWindow::setIconSlot(QList<QImage> images)
{
printf("succes!");
this->setImageListMemory(images); //save the images to memory
QGridLayout *grid = new QGridLayout; //create the grid layout I want my images to be in
// create counters to remember the row and column in the grid
int counterRow =0;
int counterColumn =0;
int counter3 =0;
int counterImages = 0;
//iterate over each image in the list
QList <QImage>::iterator x;
for(x = imageListMemory.begin(); x != imageListMemory.end(); x++)
{
MyIconClass * myLabel = new MyIconClass(this); //create an object of my own class (which is a Qlabel with an int member)
QPixmap pixmap(QPixmap::fromImage(*x)); //create a pixmap from the image in the iteration
myLabel->setPixmap(pixmap); //set the pixmap on my label object
myLabel->show();
memory.append(myLabel); //add it to the memory so I can recal it
counterImages++;
}
while(counter3 < images.count())
{
grid2->addWidget(memory.value(counter3), counterRow, counterColumn);
counterColumn++;
counter3++;
if(counterColumn >= 5)
{
counterRow++;
counterColumn =0;
}
}
if(ImageHandlerMemory->layout() == 0)
{
ImageHandlerMemory->setLayout(grid2);
}
}
myiconclass.cpp
#include "myiconclass.h"
MyIconClass::MyIconClass(QWidget *parent) : QLabel(parent)
{
}
int MyIconClass::getMyNumber() const
{
return myNumber;
}
void MyIconClass::setMyNumber(int value)
{
myNumber = value;
}
As Benjamin T said I had to change this:
MyIconClass * myLabel = new MyIconClass(this);
into this:
MyIconClass * myLabel = new MyIconClass(ImageHandlerMemory);
Thanks Benjamin!
PS, I also had to add this line in my mainwindow constructor:
grid2 = new QGridLayout;
My programm can add new QLabels and QLineEdits to a QScrollArea after a button is clicked. The idea is to create a grocery list. My problem is when a second Button is clicked I want to get the text of all the QLineEdits. But I don't know how to use those elements, because every new QLineEdit-variable has the same name and I don't know how to change that.
Below is a small example:
my MainWindow.h:
#ifndef MainWINDOW_H
#define MainWINDOW_H
#include <QMainWindow>
#include <string>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int i;
private:
Ui::MainWindow *ui;
private slots:
void on_create_clicked();
read_text();
};
#endif // MainWINDOW_H
my MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_create_clicked()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(read_text()));
i = 1;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_create_clicked()
{
if(i < 10)
{
i ++;
QLabel *label_2 = new QLabel();
QString s = QString::number(zaehlerHeight) + ". ";
label_2->setText(s);
ui->scrollArea->widget()->layout()->addWidget(label_2);
QLineEdit *lineEdit = new QLineEdit();
ui->scrollArea_2->widget()->layout()->addWidget(lineEdit);
}
else{
ui->label->setText("already 10");
}
}
void MainWindow::read_text()
{
QString mytext = ui->lineEdit->text();
}
I would simply store the pointer to each QLineEdit in a QVector, and then loop in this vector to get the text of each.
Header:
#ifndef MainWINDOW_H
#define MainWINDOW_H
#include <QMainWindow>
#include <string>
#include <QVector>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int i;
private:
Ui::MainWindow *ui;
QVector<QLineEdit *> m_VecLineEdits;
private slots:
void on_create_clicked();
private:
void read_text();
void GetAllTextEdit();
};
#endif // MainWINDOW_H
In Cpp file, change the following:
void MainWindow::on_create_clicked()
{
if(i < 10)
{
i ++;
QLabel *label_2 = new QLabel();
QString s = QString::number(zaehlerHeight) + ". ";
label_2->setText(s);
ui->scrollArea->widget()->layout()->addWidget(label_2);
QLineEdit *lineEdit = new QLineEdit();
m_VecLineEdits.push_back(lineEdit); // <-- Line added here to save the pointers in a QVector.
ui->scrollArea_2->widget()->layout()->addWidget(lineEdit);
}
else{
ui->label->setText("already 10");
}
}
void MainWindow::GetAllTextEdit()
{
for(int j = 0; j<m_VecLineEdits.size(); ++j)
{
QString lineEditText = m_VecLineEdits.at(j)->text();
/* Do anything with this value */
}
}
If you delete your QLineEdit, remember to also remove them from the QVector.
If you want to change the name of the variable ( ie the pointer to the QLineEdit ) each time you slot is called, and provided that i will stay small ( < 10 ), you can use switch(i) for example and choose a different variable name for each case, but you will have to store all those variables as members of your class. So it's better to store the pointers in a QList or a QVector and loop over those containers to access the text() method on each QLineEdit.
you can't, because you don't have any pointer or reference to those objects, one solution would be having a array of QLabel in your class definition.
ex:
QVector<QLabel*> _labels;
and adding and instantiating one by one with the press of the button and then you will have the whole list of objects, thus their names
I have an mainwindow app, with 3 main modules and each modules have 6 sub-modules. Each submodel consists of different kind of Gui widgets (like Qlineedit, Qlabel, QPushbutton,QGroupBox etc…).
I would subclass all each main and sub-modules (inheriths QWidget). Each main and sub module is created dynamically. For ex, when the 1st main module is constructed, it also allocates for the 6 submodules. But, when the app is closed, it doesn’t free all 6 submodules. There may be a leakage problem but I didn’t figure out. why. I think I have a problem with subclassing of my modules.
My app is too big to post here so I would post 2 benchmark codes. To show the issue, this mainwindow app has 100 my custom QLineEdit object (I know it is weird but it adresses my problem well). But when the app is closed, doesn’t free all the 100 custom Qlineedit objects. (Here, my custom QLineedit class represents my modules…)
I have tried 2 kind of subclassing to understand the problem. 1. BcInputBox inherits QLineEdit 2. BcInputBox inherits QWidget
I’ve tried both of them separately but the issue is the same. I’ve spent more than 1 week but I haven’t figured it out yet.
Which approach would be better? What is the mistake about my design?
1st Benchmark
// #####################################################
// Benchmark 1 //
// #####################################################
//
//BcInputBox.h
#ifndef BCINPUTBOX_H
#define BBCINPUTBOX_H
#include <QLineEdit>
namespace BC {
const bool ReadOnly = true;
const bool Edit = false;
const bool Double = true;
const bool Name = false;
}
class BcInputBox: public QWidget
{
Q_OBJECT
public:
explicit BcInputBox(QWidget *parent = 0, bool editInfo = BC::Edit, bool inputInfo = BC::Double);
~BcInputBox();
void setEditInfo(bool editInfo);
void setInputInfo(bool inputInfo);
QLineEdit *getInputBox() const;
static int objCounter;
private:
QLineEdit *inputBox;
};
#endif // BCINPUTBOX_H
//******************************************
//BcInputBox.cpp
#include <QDebug>
#include <QHBoxLayout>
#include <QValidator>
#include "BcInputBox.h"
#include "BcDoubleValidator.h"
int BcInputBox :: objCounter = 0;
BcInputBox::BcInputBox(QWidget *parent, bool editInfo, bool inputInfo):QWidget(parent)
{
QHBoxLayout *hlay = new QHBoxLayout(this);
inputBox = new QLineEdit(this);
setEditInfo(editInfo);
setInputInfo(inputInfo);
hlay -> addWidget(inputBox);
inputBox -> setStyleSheet("background-color: yellow;"
"border-radius: 8px;");
qDebug() << objCounter++ << "BcInputBox()";
}
BcInputBox :: ~BcInputBox()
{
//qDebug() << objCounter ;
qDebug() << "~BcInputBox";
delete inputBox;
}
void BcInputBox :: setEditInfo(bool editInfo)
{
inputBox -> setReadOnly(editInfo);
}
void BcInputBox :: setInputInfo(bool inputInfo)
{
if (!inputInfo){
QRegExp rExp ("[A-Za-z0-9]{1,16}");
inputBox -> setValidator(new QRegExpValidator(rExp, inputBox));
}
else {
inputBox -> setValidator( new BcDoubleValidator( 0.0, 1000, 3,inputBox));
}
}
QLineEdit *BcInputBox :: getInputBox() const
{
return inputBox;
}
//******************************************
//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLineEdit>
#include "subconsmod1.h"
#include "BcInputBox.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
BcInputBox *subSection[100];
};
#endif // MAINWINDOW_H
//******************************************
//MainWindow.cpp
#include <QtCore>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVBoxLayout *hlay = new QVBoxLayout(ui->centralWidget);
for (int i = 0; i < 100; ++i ){
subSection[i] = new BcInputBox (ui->centralWidget);
hlay -> addWidget(subSection[i]);}
ui->centralWidget ->setLayout(hlay);
}
MainWindow::~MainWindow()
{
delete ui;
}
2nd Benchmark
// #####################################################
// Benchmark 2 //
// #####################################################
//
//BcInput.h
#ifndef BCINPUT_H
#define BCINPUT_H
#include <QLineEdit>
class BcInput : public QLineEdit
{
Q_OBJECT
public:
explicit BcInput(QWidget *parent = 0);
~BcInput();
static int counter;
signals:
public slots:
};
#endif // BCINPUT_H
//******************************************
//BcInput.cpp
#include <QDebug>
#include "bcinput.h"
int BcInput :: counter = 0;
BcInput::BcInput(QWidget *parent) :
QLineEdit(parent)
{
qDebug() << "BcInput()";
qDebug() << counter++;
}
BcInput :: ~BcInput()
{
qDebug() << "~BcInput()";
}
//******************************************
//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "bcinput.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
BcInput *subSection[100];
};
#endif // MAINWINDOW_H
//******************************************
//MainWindow.cpp
#include <QtGui>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "bcinput.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVBoxLayout *hlay = new QVBoxLayout(ui->centralWidget);
for(int i = 0; i<100;i++){
subSection[i] = new BcInput(ui->centralWidget);
hlay->addWidget(subSection[i]);}
ui->centralWidget->setLayout(hlay);
}
MainWindow::~MainWindow()
{
for(int i = 0; i<100;i++)
delete subSection[i];
delete ui;
}