Pete morgan:Edit user dialog

From qtnode

Jump to: navigation, search
Image:Simple_users_edit_dialog.png

This is a simple dialog for editing an user. Its assumed the database is connected elsewhere with QSqlDatabase. The users details are contained in an User class with load() and save() methods that connect to the database. The Group dropdown box is populated from a query within the dialog.

The dialog could be invoked with

int user_id = 200;
EditUserDialog usersDialog(user_id);
int result = usersDialog.exec();
if(result){
   // Save was pressed so do something eg update view
   doSomething()
}


User class

Here's the "user.h" file showing the User properties and methods.

class User
{
  public:
    int     user_id;
    QString name;
    int     group_id;
    QString group_name;
    bool    active;
    bool    admin;

    User();
    void load( int user_id );
    void save();
};

Header file

The EditUserDialog.h header file.

#ifndef _EDIT_USER_DIALOG_H
#define _EDIT_USER_DIALOG_H
 
#include <QDialog>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QComboBox>
#include <QLineEdit>
#include <QCheckBox>
#include "user.h"
   
class EditUserDialog : public QDialog
{
   Q_OBJECT
public:
   EditUserDialog(int user_id, QDialog *parent = 0);
  
private:
   User user;  // class object
   // the widgets
   QLineEdit *wUser;
   QComboBox *wGroup;
   QCheckBox *wActive;
   QCheckBox *wAdmin;
  
private slots:
   // Widget events
   void userTextChanged(QString newText);
   void groupDropdownChanged(int newIndex);
   void activeClicked(int state);
   void adminClicked(int state);
   // Save, Cancel events
   void accept();
   void reject();
};
#endif

Implementation

Image:Simple_users_edit_dialog.png

The EditUserDialog.cpp file. Every time a widget is changed then the properties of the User class are updated.

#include "edit_user_dialog.h"
  
// Dialog constructor
EditUserDialog::EditUserDialog(int user_id, QDialog *parent) : QDialog(parent)
{
   user.load(user_id);  // Load user details
   
   setModal(true);
   // Set the dialog title to new or edit - zero is a new user
   setWindowTitle( (user_id > 0) ? "Edit User" : "New User" );
   
   // Main vertical box will contain grid of widgets at top and buttons below
   QVBoxLayout *mainBox = new QVBoxLayout;
   mainBox->setMargin(20);     // Add some space around the sides
   setLayout(mainBox);	        // layout for the dialog
   
   // Grid for the user widgets
   QGridLayout *grid = new QGridLayout();
   grid->setSpacing(10);       // Space the widgets 
   mainBox->addLayout( grid ); // Add to the top of the layout
   
   // Username label and text widget
   grid->addWidget( new QLabel("User name"), 0, 0 ); // label at lst row, 1st column
   wUser = new QLineEdit( user.name ); 
   grid->addWidget( wUser, 0, 1 );
   connect(wUser, SIGNAL( textChanged(QString) ), this, SLOT( userTextChanged(QString) ) ); 
   
   // Group label and dropdown
   grid->addWidget( new QLabel("Group"), 1, 0 );   // label at 2nd row, 1st column
   wGroup = new QComboBox();
   QSqlQuery sql("select group_id, group_name from groups order by group_name asc");
   while( sql.next() ){
       // first parmater is the text that appears, the second is the data ie group_id
       wGroup->addItem( sql.value(1).toString(), sql.value(0).toInt() );
   }
   // Next line selects the dropdown item to match the current group_id
   wGroup->setCurrentIndex( wGroup->findData(user.group_id) );
   grid->addWidget( wGroup, 1, 1 );    // 2nd row, 2nd column
   connect(wGroup, SIGNAL( currentIndexChanged(int) ), this, SLOT( groupDropdownChanged(int) ) );
   
   // Active checkbox
   wActive = new QCheckBox("Active on system");
   wActive->setChecked( user.active );
   grid->addWidget( wActive, 2, 1 );   // 3rd row, second column
   // Note that connect() is after setChecked(). If this connect() had occurred
   // before setChecked(), then the slot would have been called.
   connect(wActive, SIGNAL( stateChanged(int) ), this , SLOT( activeClicked(int) ) );
   
   // Admin checkbox
   wAdmin = new QCheckBox("Has admin rights");
   wAdmin->setChecked( user.admin );
   grid->addWidget( wAdmin, 3, 1 );    // 4th row, second column
   connect(wAdmin, SIGNAL( stateChanged(int) ), this , SLOT( adminClicked(int) ) );
  
   // Save and Cancel Buttons at bottom
   QDialogButtonBox *buttonBox = new QDialogButtonBox(	QDialogButtonBox::Save |
                                                       QDialogButtonBox::Cancel );
   // Connect the buttonBox events
   connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
   connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
   mainBox->addWidget( buttonBox );
}
   
// User name text has changed 
void EditUserDialog::userTextChanged(QString newText)
{
   user.name = newText;
}
   
// Group dropdown changed
void EditUserDialog::groupDropdownChanged(int newIndex)
{
   user.group_id = wGroup->itemData(newIndex).toInt();
}
  
// Active checkbox clicked 
void EditUserDialog::activeClicked(int newState)
{
   user.active = newState;
}
 
// Admin checkbox clicked 
void EditUserDialog::adminClicked(int newState)
{
   user.admin = newState;
}
 
// Cancel button pressed
void EditUserDialog::reject()
{
   close();
}

// Save button pressed
void EditUserDialog::accept()
{
   user.save();   // Call user class function to save users details
   setResult( QDialog::Accepted ); // Send Accepted back to caller
   close();
}
Personal tools