Reputation: 77
I am an application using Qt Creator in C ++. The int main () function is as follows. I am starting a QDialog Window where I am getting the "url" variable content.
How can I get this variable into the mainwindow.cpp file.
QApplication a(argc, argv);
MainWindow *w; //Put it here in case you have several cases where you want to initialize it
QLoginDialog d; //Your login dialog
if(d.exec()==QDialog::Accepted)
{
if(d.isLogged()) //Check the variable used to validate the login
{
w = new MainWindow;
w->show();
}
else
{
//Handle bad login
return -1; //Don't forget to return something here
}
}
else
{
//Handle dialog not validated
return -1; //Don't forget to return something here
}
return a.exec();
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Uİ::MainWindow)
{
QDialog s;
s.show();
s.setmodel(true);
if(s.exec()==QDialog::Accepted)
{
if(s.URL=="true")
{
ui.setupUi(this);
}
}
}
QDialog.cpp=Start.cpp source code:
#include "start.h"
#include "ui_start.h"
Start::Start(QWidget *parent) :
QDialog(parent),
ui(new Ui::Start)
{
ui->setupUi(this);
}
Start::~Start()
{
delete ui;
}
void Start::on_pushButton_2_clicked()
{
QString dirName=QFileDialog::getExistingDirectory(
this,
tr("Select a Directory"),
QDir::currentPath());
if(!dirName.isNull())
ui->lineEdit_2->setText(dirName);
}
void Start::on_pushButton_3_clicked()
{
URL=ui->lineEdit->text();
Directory=ui->lineEdit_2->text();
if(URL.isEmpty() && Directory.isEmpty())
{
QMessageBox::warning(this,tr("Error"),tr("Please, fill in the blanks"));
}
else if(URL.isEmpty())
{
QMessageBox::warning(this,tr("Error"),tr("is not a valid URL or Path!!"));
}
else if(Directory.isEmpty())
{
QMessageBox::warning(this,tr("Error"),tr("is not a select directory!!"));
}
ControlConfiguration();
}
void Start::ControlConfiguration()
{
QString configPosition= ui->lineEdit_2->text();
QString str1="CHECKOUT_HOME";
QString str2="new_checkout.csh";
QString str3="setup_prjsvn.csh";
QMessageBox *msgBox=new QMessageBox(this);
int sayac=0;
QDir path(configPosition);
QStringList files=path.entryList(QDir::Files);
for(int i=0; i<files.length(); i++)
{
if(files[i].compare(str1)==0)
{
sayac=sayac+1;
}
}
for(int i=0; i<files.length(); i++)
{
if(files[i].compare(str2)==0)
{
sayac=sayac+1;
}
}
for(int i=0; i<files.length(); i++)
{
if(files[i].compare(str3)==0)
{
sayac=sayac+1;
}
}
if(sayac>=3)//Attention Flurent. if true, open QMainWindow
{
QMessageBox::information(this,tr(" "),tr("Configuration Directory"));
//s_path=ui->lineEdit->text();
this->hide();
s_path="true";
URL=ui->lineEdit_2->text();
}
else
{
msgBox->setInformativeText("Would you like to configure?");
msgBox->setStandardButtons(QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes);
msgBox->setDefaultButton(QMessageBox::Yes);
int ret = msgBox->exec();
switch(ret){
case QMessageBox::Yes:
Configuration();
break;
case QMessageBox::No:
//ui->locationEdit->clear();
break;
case QMessageBox::Cancel:
break;
}
}
}
void Start::Configuration()
{
QString projePosition= ui->lineEdit_2->text(); // Proje konumu alınır.
QProcess* process=new QProcess(this);
QMessageBox *msgBox=new QMessageBox(this);
process->setWorkingDirectory(projePosition);
QString svn_export="svn export http://atlas/svn/prjsvn/trunk/common/common_bin/istanbul/new_checkout.csh";
QString s_newcheckout="source new_checkout.csh";
QString s_prjsvn="source setup_prjsvn.csh";
process->start("tcsh -c \""+svn_export+";"+s_newcheckout+";"+s_prjsvn+"\"");
process->waitForBytesWritten();
process->waitForFi
}
Upvotes: 2
Views: 3673
Reputation: 834
You should have a proper look at signals and slots, to pass variables between objects. The documentation is here : http://doc.qt.io/qt-4.8/signalsandslots.html
An other way would be to execute the QDialog inside your MainWindow, that would be the best practice. Use controls in the scope where they are useful. You should not have to use dialogs in the main function.
So, have a look at both solutions. The best, for simple variables, being to execute your dialog inside the class where the value will be used, the second being the use of signals and slots if values have to be passed between complex classes.
main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow *w; //Put it here in case you have several cases where you want to initialize it
QLoginDialog d; //Your login dialog
if(d.exec()==QDialog::Accepted)
{
if(d.isLogged()) //Check the variable used to validate the login
{
w = new MainWindow(d.getUrl()); //I guess the URL comes from your login dialog, so make a function that returns it
w->show();
}
else
{
//Handle bad login
return -1; //Don't forget to return something here
}
}
else
{
//Handle dialog not validated
return -1; //Don't forget to return something here
}
return a.exec();
}
mainwindow.cpp
//Don't forget to add the "QString sUrl" to your mainwindow.h
MainWindow::MainWindow(QString sUrl, QWidget *parent): QMainWindow(parent), ui(new Uİ::MainWindow)
{
ui.setupUi(this);
qDebug()<<s.URL; //It will be displayed here
}
Your dialog.cpp
//Use this to return the boolean value that represents if the path is valid or not.
bool QDialog::isLogged()
{
return this->s_path.equals("true");
}
Upvotes: 3
Reputation: 331
A standard generated main file for a qt application contains the following lines:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
The main window is created there.
So in your case you could create the main window before your return. By adding a QString as parameter in the constructor the url would be available in the mainwindow.
so the mainWindow.cpp constructor changes to:
MainWindow::MainWindow(QString url, QWidget *parent) :
QMainWindow(parent), ui(new Uİ::MainWindow)
{
ui.setupUi(this);
//Place your code here that uses the url,
}
so the mainWindow.h constructor changes to:
MainWindow::MainWindow(QString url = "", QWidget *parent = 0);
and the main.cpp changes to:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString url; //Fill this from your dialog.
MainWindow w(url);//Pass the url to the main window
w.show();
return a.exec();
}
Upvotes: 2
Reputation: 251
If all you want to do is to ask for an input string, i suggest you to have a look at QInputDialog
Example:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
bool ok;
QString message = QInputDialog::getText
(0, "Input","URL:", QLineEdit::Normal,"", &ok);
qDebug()<<message;
ui->setupUi(this);
}
Upvotes: 0