HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Dynamically adding QLineEdits and then copy text from them to vector in right order

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
orderdynamicallytextrightqlineeditsaddingthenandfromthem

Problem

Here is how it looks at the beginning:

And after I can add random counts of line edits:

Or delete them:

The main purpose is the possibility to add or delete QlineEdits which stores user names and each QlineEdit adds a username to the vector in the right order (index) like this:

users =>
 [0] = "a";
 [1] = "b";
 [2] = "c";
 [3] = "d";  
 [4] = "e";
 [5] = "f";
 [6] = "g";


Here is the code :

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void addbutton_Clicked();
    void deletebutton_Clicked();
    void lineedit_textChanged(QString text);

private:
    QVBoxLayout *middlelayout;
    QList qlist;
    std::vector users;
};

#endif // MAINWINDOW_H


mainwindow.cpp

```
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget window = new QWidget(this);
QVBoxLayout *mainlayout = new QVBoxLayout;
middlelayout = new QVBoxLayout;
QHBoxLayout *buttonlayout = new QHBoxLayout;
QPushButton *addbutton = new QPushButton;
QPushButton *deletebutton = new QPushButton;

addbutton->setText("ADD");
deletebutton->setText("Delete");

buttonlayout->addWidget(addbutton);
buttonlayout->addWidget(deletebutton);
buttonlayout->setAlignment(Qt::AlignTop);
mainlayout->addLayout(buttonlayout);
mainlayout->addLayout(middlelayout);
window->setLayout(mainlayout);
setCentralWidget(window);

connect(addbutton, SIGNAL(clicked()), SLOT(addbutton_Clicked()));
connect(deletebutton, SIGNAL(clicked()), SLOT(deletebutton_Clicked()));
}

MainWindow::~MainWindow()
{
}

void MainWindow::addbutton_Clicked()
{
QLineEdit *lineedit = new QLineEdit;
qlist.push_back(lineedit);
middlelayout->addWidget(lineedit);
lineedit->setObjectName("lineedit

Solution

Since the MainWindow destructor is empty, you can just omit it.

In lineedit_textChanged, you declare the index variable at the top,
but it's only used if the text has changed.
It would be better to declare it inside the if branch where you actually use it:

if (isTextchanged)
{
    QStringList list = senderobjName.split("lineedit");
    unsigned int index = list[1].toInt();
    users[index] = text;
}


This snippet can be a bit confusing without braces on the outer if:

if(!middlelayout->isEmpty())
    if((child = middlelayout->takeAt(qlist.count()-1)->widget()) != 0) {
        delete child;
        qlist.removeLast();
}


It would be clearer this way:

if (!middlelayout->isEmpty()) {
    if ((child = middlelayout->takeAt(qlist.count()-1)->widget()) != 0) {
        delete child;
        qlist.removeLast();
    }
}

Code Snippets

if (isTextchanged)
{
    QStringList list = senderobjName.split("lineedit");
    unsigned int index = list[1].toInt();
    users[index] = text;
}
if(!middlelayout->isEmpty())
    if((child = middlelayout->takeAt(qlist.count()-1)->widget()) != 0) {
        delete child;
        qlist.removeLast();
}
if (!middlelayout->isEmpty()) {
    if ((child = middlelayout->takeAt(qlist.count()-1)->widget()) != 0) {
        delete child;
        qlist.removeLast();
    }
}

Context

StackExchange Code Review Q#78128, answer score: 3

Revisions (0)

No revisions yet.