PDA

View Full Version : Showing form - help



waynew
27th October 2009, 02:00
I hate to ask for help on something this simple, but I'm trying to learn C++ and Qt at the same time - can't find an answer in any of my books or online, so...

I have a dialog (stninfodialog) that works fine when called from a menu pick on the main window.
Now I need to call that dialog from a function in a different class.
Simple in Java, but... I can't see how to do it in Qt/C++.

Here is the relevant code from the main window cpp:



#include <QtGui>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "makestndb.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
setupActions();
mStatLabel = new QLabel;
statusBar()->addPermanentWidget(mStatLabel);
connect(ui->call, SIGNAL(textChanged()), this, SLOT(updateStats()));
StnInfoD = new StnInfoDialog;
bool chkStnInfo = makeStnDB();
}

void MainWindow::setupActions()
{
connect(ui->actionStn_Info, SIGNAL(triggered(bool)),
this, SLOT(stnInfo()));
}

void MainWindow::stnInfo()
{
StnInfoD->show();
}



So, now the other class/function (makestndb.h/makestndb.cpp):

makestndb.h



#ifndef MAKESTNDB_H
#define MAKESTNDB_H

#include "stninfodialog.h"

class makeStnDB {
private:
StnInfoDialog *StnInfoD;

};

bool makeStnDB();

#endif // MAKESTNDB_H


makestndb.cpp



#include "makeStnDB.h"
#include <iostream>
#include <QString>
#include <QtSql>
#include "stninfodialog.h"

bool makeStnDB() {
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("StnInfo");
db.open();

//Check to see if stninfo exists...
QSqlQuery query = QSqlQuery::QSqlQuery("StnInfo");
query.exec("SELECT call FROM stnInfo");

if (query.isNull(1)) {
query.exec("CREATE table stnInfo (id int recNum primary key, call varchar(8), "
"name varchar(80), address1 varchar(80), accress2 varchar(80), "
"city varchar(80), state varchar(80), state varchar(40), postalCode varchar(20), "
"country varchar(40), grid varchar(8), cqZone varchar(8), ituZone varchar(8), "
"latitude varchar(8), longitude varchar(8), licenseClass varchar(12)");

//HOW DO I CALL THE STNINFODIALOG FROM HERE?

}
if (!db.open()) {
return false;
}
return true;
}


Thanks for any advice!

wysota
27th October 2009, 10:32
Does it even compile?

Anyway, in C++ you can do it the same as in Java. The concept will be the same although the implementation of the concept will differ a bit.

jano_alex_es
27th October 2009, 10:44
Now I need to call that dialog from a function in a different class.

Few ways... I use to create classes which inherits from QDialog and I include its header file wherever I want to create a dialog. Actually, in my opinion this is the cleaner way.

But you can implement a public function which will give a pointer to the already created (or NULL if not) QDialog.

waynew
28th October 2009, 02:04
Thanks guys, I got it working. Lots to learn.