5
\$\begingroup\$

Here is how it looks at the beginning:

enter image description here

And after I can add random counts of line edits:

enter image description here

Or delete them:

enter image description here

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 <QMainWindow>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QList>
#include <vector>
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<QLineEdit*> qlist;
    std::vector<QString> 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"+QString::number(qlist.count()));

    users.resize(qlist.count());
    connect(lineedit, SIGNAL(textChanged(QString)), SLOT(lineedit_textChanged(QString)));
}

void MainWindow::deletebutton_Clicked()
{
    QWidget *child;
    if(!middlelayout->isEmpty())
        if((child = middlelayout->takeAt(qlist.count()-1)->widget()) != 0) {
            delete child;
            qlist.removeLast();
    }
}

void MainWindow::lineedit_textChanged(QString text)
{
    unsigned int index = 0;
    QObject *obj = QObject::sender();
    QString senderobjName = obj->objectName();
    int isTextchanged = senderobjName.indexOf("lineedit") != -1;

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

The code works fine but it seems that I'm doing it wrong and there is a better way.

\$\endgroup\$
1
  • \$\begingroup\$ From your screenshots it seems that if you add 100 line edits and then delete 99 the remaining line edit will be very far down. Just a UX issue. \$\endgroup\$
    – cheezsteak
    Jan 20, 2015 at 15:57

1 Answer 1

3
\$\begingroup\$

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();
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.