-1

I am working on signal and slots.

here is the mainwindow.h

....
public slots:
void slotChangeName();
....

mainwindow.cpp;

#include<globals.h>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{  
  QObject::connect(project_created,SIGNAL(selectionChanged()),this,SLOT(slotChangeName()))
}
void MainWindow::slotChangeName()
{
  ui->project_name->setText(project_directory);
}

When a project created, the global variable, "project_created",is updated as 1. I want to write the project directory on the label when "project_created" updated. What do I have to do?

globals.h

#ifndef GLOBALS_H
#define GLOBALS_H

class QString;

extern int project_created;
extern QString project_directory;

#endif 

globals.cpp

#include "globals.h"
#include <QString>

// ALL THE GLOBAL DEFINITIONS

int project_created = 0;
QString project_directory = "";

When people clicked to the new project, they can create a project folder. After that the project_created updated as 1. I want to write the project name nnext to the yellow folder icon.

enter image description here

ymoreau
  • 3,402
  • 1
  • 22
  • 60
dizel
  • 25
  • 6
  • what type is project_created, and where is it defined? same for project_directory – Caleth May 22 '17 at 13:45
  • They are global variables.I have defined them in the global class and included them into the main class. Project_created is int @Caleth – dizel May 22 '17 at 13:46
  • Is project_created a `QAbstractItemView *` or similar pointer? You are currently only calling slotChangeName when the user selects something inside whatever project_created is – Caleth May 22 '17 at 13:51
  • @Caleth I have edited the code. You can see the declerations. – dizel May 22 '17 at 13:59

1 Answers1

0

You need a pair[1] of QObjects to use QT's signals and slots mechanism. An int variable changing doesn't magically cause code to be executed, which is what it seems like you are trying here.

Your "Create Project" dialog is a reasonable place to define a signal, which MainWindow can connect to.

class ProjectCreateDialog : ... {
    ...
signals:
    void projectCreated(QString);
    ...
}

class MainWindow : ... {
    ...
    void createProject();
    public slots:
    void slotChangeName(QString project_directory);
    ...
}

void MainWindow::createProject()
{
    ProjectCreateDialog dialog;
    connect(&dialog, SIGNAL(projectCreated(QString)), this, SLOT(onChangeName(QString)));
    dialog.exec();
}

void MainWindow::onChangeName(QString project_directory)
{
    ui->project_name->setText(project_directory);
}

[1] well, you can use the same QObject twice

Caleth
  • 52,200
  • 2
  • 44
  • 75