namespace problems
-
Hi Geeks,
lately I have been ill-treated to easy languages (I will not name not to anger anyone) and with C++-Qt I struggle
My problem, I think is namespace clause.
I have a problem with my code but I I’m not able to fix.
mainwindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QVector> #include "task.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void addTask(); private: Ui::MainWindow *ui; QVector<Task*> mTasks; }; #endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow), mTasks() { ui->setupUi(this); connect(ui->addTaskButton, &QPushButton::clicked, this, &MainWindow::addTask); } MainWindow::~MainWindow() { delete ui; } void MainWindow::addTask(){ qDebug() << "User clicked"; Task* task = new Task("Untitled task"); mTasks.append(task); ui->tasksLayout->addWidget(task); }
the problem is with my variable "ui" I guess is a problem of definition in namespace
Thanks in advance
-
Hi Geeks,
lately I have been ill-treated to easy languages (I will not name not to anger anyone) and with C++-Qt I struggle
My problem, I think is namespace clause.
I have a problem with my code but I I’m not able to fix.
mainwindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QVector> #include "task.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void addTask(); private: Ui::MainWindow *ui; QVector<Task*> mTasks; }; #endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow), mTasks() { ui->setupUi(this); connect(ui->addTaskButton, &QPushButton::clicked, this, &MainWindow::addTask); } MainWindow::~MainWindow() { delete ui; } void MainWindow::addTask(){ qDebug() << "User clicked"; Task* task = new Task("Untitled task"); mTasks.append(task); ui->tasksLayout->addWidget(task); }
the problem is with my variable "ui" I guess is a problem of definition in namespace
Thanks in advance
@grafenocarbono
What line do you get what error message on?If you are using
ui
, that normally comes from using Designer to create a form. That produces aui_....h
file at build time, and Creator puts a#include "ui_....h"
statement in your.cpp
, but I do not see that? -
Hi Geeks,
lately I have been ill-treated to easy languages (I will not name not to anger anyone) and with C++-Qt I struggle
My problem, I think is namespace clause.
I have a problem with my code but I I’m not able to fix.
mainwindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QVector> #include "task.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void addTask(); private: Ui::MainWindow *ui; QVector<Task*> mTasks; }; #endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow), mTasks() { ui->setupUi(this); connect(ui->addTaskButton, &QPushButton::clicked, this, &MainWindow::addTask); } MainWindow::~MainWindow() { delete ui; } void MainWindow::addTask(){ qDebug() << "User clicked"; Task* task = new Task("Untitled task"); mTasks.append(task); ui->tasksLayout->addWidget(task); }
the problem is with my variable "ui" I guess is a problem of definition in namespace
Thanks in advance
I've noticed an issue / bug with the Class wizard in Qt Creator / Designer / VS tools for Qt. Basically there's two options: include the MainWindow's auto-generated UI code as a base class and as a member (it's an option in the Class wizard dialog as you create the class).
Forgot which one worked for me, but try both, one of them will work!
Also, please refrain from the name-calling. We are all muscle men with a moderate enjoyment of the pleasures of geekdom. J/k, I thought it was funny 😂