Pokaże Tobie przykład:
listdialog.pro
#-------------------------------------------------
#
# Project created by QtCreator 2015-04-09T21:52:38
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = listdialog
TEMPLATE = app
SOURCES += main.cpp\
listdialog.cpp \
editdialog.cpp
HEADERS += listdialog.h \
editdialog.h
FORMS += listdialog.ui \
editdialog.ui
listdialog.h
#ifndef LISTDIALOG_H
#define LISTDIALOG_H
#include <QDialog>
#include "editdialog.h"
class EditDialog ;
namespace Ui {
class ListDialog;
}
class ListDialog : public QDialog
{
Q_OBJECT
public:
explicit ListDialog(QWidget *parent = 0);
~ListDialog();
private:
Ui::ListDialog *ui;
private slots :
void addItem();
void editItem();
void deleteItem();
};
#endif // LISTDIALOG_H
editdialog.h
#ifndef EDITDIALOG_H
#define EDITDIALOG_H
#include <QDialog>
namespace Ui {
class EditDialog;
}
class EditDialog : public QDialog
{
public:
explicit EditDialog(QWidget *parent = 0);
~EditDialog();
const QString name() const ;
void setName(const QString&);
const QString number() const ;
void setNumber(const QString&);
private:
Ui::EditDialog *ui;
};
#endif // EDITDIALOG_H
editdialog.cpp
#include "editdialog.h"
#include "ui_editdialog.h"
EditDialog::EditDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::EditDialog)
{
ui->setupUi(this);
}
EditDialog::~EditDialog()
{
delete ui;
}
const QString EditDialog::name() const
{
return ui->nameEdit->text().replace("--","");
}
void EditDialog::setName(const QString &name)
{
ui->nameEdit->setText(name);
}
const QString EditDialog::number() const
{
return ui->numberEdit->text().replace("--","");
}
void EditDialog::setNumber(const QString &name)
{
ui->numberEdit->setText(name);
}
listdialog.cpp
#include "listdialog.h"
#include "ui_listdialog.h"
ListDialog::ListDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ListDialog)
{
ui->setupUi(this);
connect(ui->addButton,SIGNAL(clicked()),this ,SLOT(addItem()));
connect(ui->editButton,SIGNAL(clicked()) , this , SLOT(editItem()));
connect(ui->deleteButton,SIGNAL(clicked()),this , SLOT(deleteItem()));
}
ListDialog::~ListDialog()
{
delete ui;
}
void ListDialog::addItem()
{
EditDialog dlg(this);
if(dlg.exec() == Accepted)
ui->list->addItem(dlg.name() + "--" + dlg.number());
}
void ListDialog::deleteItem()
{
delete ui->list->currentItem();
}
void ListDialog::editItem()
{
if(!ui->list->currentItem())
return ;
QStringList parts = ui->list->currentItem()->text().split("--");
EditDialog dlg(this);
dlg.setName(parts[0].trimmed());
dlg.setNumber(parts[1].trimmed());
if(dlg.exec() == Accepted)
ui->list->currentItem()->setText(dlg.name() + "--" + dlg.number());
}
main.cpp
#include "listdialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ListDialog w;
w.show();
return a.exec();
}
editdialog.ui
listdialog.ui
Troche dlugi ale pokazuje jak można połączyć 2 widgety. Musisz najpierw dodać do klasy , klase class EditDialog;
Aby wywołać widget:
EditDialog dlg(this);
Jeśli chcerz dodać plik do projektu naciśnij prawym przyciskiem na nazwe pliku i wciśnij dodaj istniejące pliki...