PDA

View Full Version : [QT] QDialog



VitaliBR
1st February 2011, 22:06
I'm with a doubt stupid, but I could not solve :(

I have two windows, a QMainWindow, and another QDialog, see:


NSControl::NSControl(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
//SETUP OF UI
ui.setupUi(this);

//SETUP WINDOW(POPUP)
add = new Add(this);

//CONNECTS
connect(ui.actionSair, SIGNAL(triggered()), this, SLOT(close()) );
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(Apply()) );
connect(ui.actionSobre, SIGNAL(triggered()), this, SLOT(About()) );
connect(ui.actionAdicionar_Endere_o, SIGNAL(triggered()), this, SLOT(AddButton()) );

ui.info_tempo->setText("1min");
}

NSControl::~NSControl()
{

}

void NSControl::Apply()
{
QString tempo = ui.comboBox->currentText();

ui.info_tempo->setText(tempo);
}

void NSControl::About()
{
QMessageBox::about( this, tr("Sobre NSControl"), tr("Desenvolvido por Mateus Vitali") );
}

void NSControl::AddButton()
{
add->show();
}

Add::Add(QWidget *parent, Qt::WFlags flags)
{
//SETUP OF UI
ui.setupUi(this);

//CONNECTS
connect(ui.cancelar, SIGNAL(triggered()), this, SLOT(Cancel()) );
connect(ui.salvar, SIGNAL(triggered()), this, SLOT(Save()) );
}

Add::~Add()
{

}

void Add::Save()
{
QMessageBox::about( this, tr("Sobre NSControl"), tr("Desenvolvido por Mateus Vitali") );
}

void Add::Cancel()
{
QMessageBox::about( this, tr("Sobre NSControl"), tr("Desenvolvido por Mateus Vitali") );
this->hide();
}

When I click the Add button, it opens the QDialog (add), but when I try to click on the buttons QDialog (Save and Cancel), nothing happens, and one was to appear as I hadplanned QMessageBox

Why?

Zlatomir
1st February 2011, 22:15
salvar and cancelar i guess are QPushButtons?
If so they don't have triggered() signal, try clicked()

VitaliBR
1st February 2011, 23:03
salvar and cancelar i guess are QPushButtons?
If so they don't have triggered() signal, try clicked()

Yes, they are QPushButtons!
Sorry to mix English with my language (Portuguese)

I swapped triggered () signal by clicked () and worked ;)

Thanks

Added after 34 minutes:

I have another doubt

I'm trying to create an XML file like this:

<List>
<Client_1 E-mail="test@test.com" Phone="1235353" Name="test" IP/DNS="test"/>
<Client_2 E-mail="test@test.com" Phone="1235353" Name="test" IP/DNS="test"/>
<Client_3 E-mail="test@test.com" Phone="1235353" Name="test" IP/DNS="test"/>
<Client_4 E-mail="test@test.com" Phone="1235353" Name="test" IP/DNS="test"/>
<Client_5 E-mail="test@test.com" Phone="1235353" Name="test" IP/DNS="test"/>
</List>

I'm doing well:

void Add::Save()
{
QDomDocument doc("List");
QDomElement root = doc.createElement("List");
doc.appendChild(root);

QDomElement cn = doc.createElement("Client_1");

cn.setAttribute("Phone", ui.phone->text());
cn.setAttribute("E-mail", ui.email->text());
cn.setAttribute("Name", ui.name->text());
cn.setAttribute("IP/DNS", ui.ip->text());

root.appendChild(cn);

QFile file1("List.xml");
if( !file1.open(QIODevice::Append) ) { }

QTextStream ts( &file1 );
ts << doc.toString();

file1.close();
}

But when an information unless I close the program and open again to save another information it looks like this:

<!DOCTYPE List>
<Lista>
<Client_1 E-mail="test@test.com" Phone="1235353" Name="test" IP/DNS="test"/>
</Lista>
<!DOCTYPE List>
<Lista>
<Client_1 E-mail="" Phone="" Name="333333" IP/DNS=""/>
</List>

VitaliBR
2nd February 2011, 13:02
anyone? :rolleyes:

BalaQT
2nd February 2011, 13:53
hi if u want to store 5 elements, then u need to loop tat
Bala

VitaliBR
2nd February 2011, 14:58
But I want to do this continuously,
ie I add two items today, tomorrow another 20, then 1, and so on

BalaQT
2nd February 2011, 15:05
hi,

But I want to do this continuously,
ie I add two items today, tomorrow another 20, then 1, and so on

u need set a variable and based on that u need to change the value.

from where you are generating this records? from db?

Bala

FelixB
2nd February 2011, 15:19
change your xml structure:


<List>
<Client ID=1 E-mail="test@test.com" Phone="1235353" Name="test" IP/DNS="test"/>
<Client ID=2 E-mail="test@test.com" Phone="1235353" Name="test" IP/DNS="test"/>
<Client ID=3 E-mail="test@test.com" Phone="1235353" Name="test" IP/DNS="test"/>
<Client ID=4 E-mail="test@test.com" Phone="1235353" Name="test" IP/DNS="test"/>
<Client ID=5 E-mail="test@test.com" Phone="1235353" Name="test" IP/DNS="test"/>
</List>

then you search for all Nodes "Client" and you can process them in a loop.

stampede
2nd February 2011, 15:20
QDomElement root = doc.createElement("List");
//...
//...
QFile file1("List.xml");
if( !file1.open(QIODevice::Append) ) { }

QTextStream ts( &file1 );

You are appending the full xml data (with header ect) to your already existing xml file, so this is exactly what you are gonna get, multiple xml documents in one file.
This is simple xml, so you'd rather want to convert the xml data from file to data structures in your program (eg. QList<MyDataStruct> data = readDataFromXml("List.xml");), work on those structures ( eg. data.append(new_client); ) and then save it (the whole list: old values + new added in current program session, eg. saveDataToXmlFile(data,"List.xml") ).

Another probelm is that you are creating a text stream on a file that will eventually fail to open, should be:

QFile file1("List.xml");
if( !file1.open(...) ) { return; }

VitaliBR
2nd February 2011, 15:25
Thanks for the tip friend
and then how would my code? Excuse my ignorance

stampede
2nd February 2011, 15:47
and then how would my code? Excuse my ignorance
I can only give you some tips. First of all, create a class wrapping the "Client", it should contain all the data that you want to associate with a client, so ( according to your xml file, and applying the improvement from FelixB ) should look like this:

class Client{
public:
// ... constructors, member access methods ect
protected:
uint _id;
QString _email;
// ... and other data you want
};

When saving to xml you can iterate over a list of object of class Client:


foreach( const Client& client, _clientsData ){
QDomElement cn = doc.createElement("Client");
cn.setAttribute("ID", client.id());
// .. and so on
}

Method that loads xml could return a QList of Clients (implement it yourself) ;)
I'll leave up to you what is and where should be "_clientsData", Client class implementation and other methods ect..
Again, I wont give full code ( I doubt that anyone will ), you'll learn much more implementing this yourself.