PDA

View Full Version : How to Open a New Dialog when a Row is Clicked..?



jaafari
3rd April 2014, 22:49
10248

Hello world!!

i am in new in Qt not only Qt but i am new programing and i come over here to get an answer for my question that is:

i am developing a program with a relationalship until now everything is ok -and- -by Qt- is easy to learn and understand becuase there are a lot of tutorials about everything.. so what i want to do now is.. when i click or select a row in my table then click a button another dialog should be opend and it should display only rows that have the same code in the column.. any idea please?

ChrisW67
3rd April 2014, 22:56
Connect your edit button clicked() signal to a slot. In the slot get the currentIndex() of the view, which tells you which row the table's current cell is in. (You could also use the view's selection if it allows only one row). You then use that to access the data from your model for your dialog.

jaafari
4th April 2014, 01:11
Hello!


connect(ui->editButton, SIGNAL(clicked()), this, SLOT(editDel()));


void MyForm::editDel()
{
//code:
}


my friend thanks for repling... as i said i am new to programing langouages can you please tell me what to do and explain the code.. thanx

ChrisW67
4th April 2014, 01:37
It depends quite a bit on what the dialog should do. Here is the sort of thing I meant:


void MyForm::editDel()
{
const QModelIndex index = ui->tableView->currentIndex();
if (index.isValid()) { // i.e. there is a current index
MyDialog dialog(ui->tableView->model(), index.row(), this);
dialog.exec();
}
}

where I assumed a customised, single purpose dialog that takes a model and row at construction. That is, its constructor looks like:


class MyDialog: public QDialog
{
Q_OBJECT
public:
MyDialog(QAbstractItemModel *model, int row, QWidget *p = 0);
...
};

jaafari
4th April 2014, 21:44
Hello!

i actually added the code to my forms but i do not know what should be changed.. so i got errors..

please help thanks

Added after 17 minutes:

should i put all the code?

jaafari
5th April 2014, 23:42
can anyone give me an exampl how to do that in both cpp files & h files ....please help

ChrisW67
6th April 2014, 00:34
You already have an example for launching a dialog. If you want to display the same model but filter the rows to show only those containing a certain value then you can use QSortFilterProxyModel on top of the main model.

jaafari
6th April 2014, 19:27
My Code:


cpp.file


Deliveries::Deliveries(QWidget *parent) :
QDialog(parent),
ui(new Ui::Deliveries)
{

ui->setupUi(this);

//-------------- connection settings-------------
Login conn;
if(!conn.connOpen())
ui->label_deliveries_statut->setText("Failed to open the database");
else
ui->label_deliveries_statut->setText("Connected....");
//-------------- connection settings-------------

deliveryModel = new QSqlRelationalTableModel(this);
deliveryModel->setTable("deliveries");
deliveryModel->select();

deliveryModel->setRelation(deliveryModel->fieldIndex("supplierid"), QSqlRelation("suppliers","id","name"));
deliveryModel->setRelation(deliveryModel->fieldIndex("productid"), QSqlRelation("products","id","name"));
deliveryModel->setRelation(deliveryModel->fieldIndex("tva"), QSqlRelation("tva","unity","type"));
deliveryModel->setRelation(deliveryModel->fieldIndex("departmentid"), QSqlRelation("department","id","name"));
deliveryModel->setRelation(deliveryModel->fieldIndex("deliverycodeid"), QSqlRelation("deliverycode","id","code"));

deliveryModel->setEditStrategy(QSqlRelationalTableModel::OnManual Submit);



ui->deliveryView->setModel(deliveryModel);

ui->deliveryView->setItemDelegate(new QSqlRelationalDelegate(this));
ui->deliveryView->setAlternatingRowColors(true);
ui->deliveryView->setCornerButtonEnabled(true);

ui->deliveryView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->deliveryView->setSelectionBehavior(QAbstractItemView::SelectRows );
ui->deliveryView->setSortingEnabled(true);
ui->deliveryView->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
ui->deliveryView->horizontalHeader()->setStretchLastSection(true);
ui->deliveryView->resizeColumnsToContents();
ui->deliveryView->setCurrentIndex(deliveryModel->index(0, 0));

ui->deliveryView->setColumnHidden(0/*Deliveries_Id*/, true);


connect(ui->editButton, SIGNAL(clicked()), this, SLOT(editDel()));

}

Deliveries::~Deliveries()
{
delete ui;
}

void Deliveries::editDel()
{
const QModelIndex index = ui->deliveryView->currentIndex();
if (index.isValid()) { // i.e. there is a current index
Dialog dialog(ui->deliveryView->deliveryModel(), index.row(), this);
dialog.exec();
}
}


header.file

namespace Ui {
class Deliveries;

}

class Deliveries : public QDialog
{
Q_OBJECT

public:
explicit Deliveries(QWidget *parent = 0);
~Deliveries();
private slots:
void updateDel();

private:
Ui::Deliveries *ui;


QSqlRelationalTableModel *deliveryModel;

};

#endif // DELIVERIES_H



i do not know what next...

jaafari
20th April 2014, 21:51
where should i use QSortFilterProxyModel in my dialog.cpp or in myForm.cpp ??

ChrisW67
22nd April 2014, 22:49
Assuming the Dialog displays everything in the model you pass it, you want to filter that before you give it to the Dialog constructor. Create a QSortFilterProxyModel, set its filter, and pas that to the Dialog.