PDA

View Full Version : Call to QDialog opens 2 windows



rdjenner
13th August 2010, 23:36
I am new to qt so please forgive my ignorance. I have created a dialog box using qt creator. When I call this from my main window which I have a button for, the call opens up my qdialog box but also a new dialog box. Not sure what is going on. Here is the revelant parts of my code

itemmaintenance.cpp

void ItemMaintenance::on_pricingButton_clicked()
{
// itemPrice i;
itemPrice *ip = new itemPrice();
ip->show();
}

itemPrice.h
#ifndef ITEMPRICE_H
#define ITEMPRICE_H

#include <QDialog>
#include "ui_itemPrice.h"
#include <QTableView>
#include <QtSql>

namespace Ui {
class itemPrice;
}

class itemPrice : public QDialog
, public Ui::itemPrice
{
Q_OBJECT

public:
itemPrice(QWidget *parent = 0);
virtual ~itemPrice();

private:
Ui::itemPrice *ui;
QSqlTableModel *ipmodel;
QTableView *iptableView;

private slots:
void loadData(QString);
};

#endif // ITEMPRICE_H

itemprice.cpp
#include "itemprice.h"

itemPrice::itemPrice(QWidget *parent) :
QDialog(parent)
,ui(new Ui::itemPrice)
{
ui->setupUi(this);
// setupUi(this);
loadData(" ");
}

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

void itemPrice::loadData(QString partNumber)
{

ipmodel = new QSqlTableModel;
ipmodel->setTable("in_price");
ipmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
ipmodel->setSort(2,Qt::AscendingOrder);
if(partNumber > " ") ipmodel->setFilter(QString("partNumber = '%1'").arg(partNumber));
ipmodel->select();

iptableView = new QTableView;
iptableView->close();
iptableView->setModel(ipmodel);
iptableView->setColumnHidden(0,true);
iptableView->setColumnHidden(1,true);
iptableView->show();
}

Please help.

Zlatomir
13th August 2010, 23:59
It looks like this is the issue:


iptableView = new QTableView;
// don't you want something like:
// iptableView = new QTableView(this); //and don't show() it separate?

//*** don't forget to use layouts ***
iptableView->close();
iptableView->setModel(ipmodel);
iptableView->setColumnHidden(0,true);
iptableView->setColumnHidden(1,true);
iptableView->show(); //here you show some widget that, i think, you want to be a child of your dialog


Anyway, i don't really understood your design, if you created the dialog in the Designer, why are you adding widgets with code? You could have added that QTableView from the designer.

***i'm not sure that you can add widgets to the designer created dialog with using layouts (i never done that before), if you can't do it, you can add the QTableView in the designer

Also ipmodel = new QSqlTableModel; should have a parent, or else you should manually delete the pointer.

rdjenner
14th August 2010, 00:21
I changed the code for the qtableview to add this and it got me closer. If I understand what you are saying, the fact that I added a table view in the creator and called it iptableView, why am I adding it again with the the new? What happens now is that the window opens up with 2 tableviews in it. When I remove the iptableview = new QTableView(this) and click the button to call the form, the application errors out.

rdjenner
14th August 2010, 00:30
You are right with regard the last statement

10.iptableView->show(); //here you show some widget that, i think, you want to be a child of your dialog

This is the object name of the table view on my dialog window (iptableView). But it appears to create a new tableview and ignore the one on my dialog window.

Zlatomir
14th August 2010, 00:38
//iptableView->show(); //comment this line


Comment that line (and see what happens), maybe you don't have two widgets there (but it look like you have, because you have QTableView *iptableView; in your class declaration and you initialise that not the one that you said you put in your designer .ui file) also next time use [ code ] and [ /code ] around code (without spaces)

LE:
Use:


//iptableView = new QTableView; // comment this
//ui->iptableView_Name_from_designer->close(); // comment this

//the following will use the QTableView you added to the designer
ui->iptableView_Name_from_designer->setModel(ipmodel);
ui->iptableView_Name_from_designer->setColumnHidden(0,true);
ui->iptableView_Name_from_designer->setColumnHidden(1,true);
//iptableView->show(); // commented

also comment from the header file the line: //QTableView *iptableView;

And don't forget the parent for your model, or manually delete, or you will have a leak

rdjenner
14th August 2010, 00:54
That did it. Thank you SOOOOOOOOOOOOOOOOOOOOO much.