Newbie Tabpage delete
-
Hi all,
I'm a developer that mainly works with Powerbuilder and .NET/C# on Windows but with some C experience in the past. I was looking for a development framework/tools to migrate some personal applications to Linux and come to QT. At the moment, after reading some tutorials, books and done some test, I'm very impressed with the possibilities and how easy it's to do some thinks with QT.
Now I have started to port a simple aplication to QT and have some questions.
My main window has a ListWidged on the left side and a TabWidget on the CentralWidget.
On the ListWidged I have my menu options and if the user clics on one item, I open a new tabpage on the TabWidged with the form. I allow only one tab for every item on le ListWidged and the user can close the tabpage.
I have already coded this functionality but I'm sure it's not optimal, therefore I need your help to improve my learning curve.
First question: Is it necessary to delete/destroy my tabpage after closing it with remove tab? Are my code OK?
To open the forms, I have declared a pointer in my mainwindow for every different form I can open. Is there a general solution/function to add a new form to the tab. Please look at my code. I want to avoid the openFormAccount and the switch on item changed slot, and have a general openForm method. How can I do it?
This is my code:
@
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include "formaccounts.h"
#include <QListWidgetItem>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow ui;
FormAccounts formAccounts;private slots:
void listWidgetCurrentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
void openFormAccounts();
void removeTab(int index);
};
@@
#endif // MAINWINDOW_H//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QStringList>
#include <QDebug>MainWindow::MainWindow(QWidget parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem,QListWidgetItem*)), this, SLOT(listWidgetCurrentItemChanged(QListWidgetItem*,QListWidgetItem*)));
formAccounts = 0;
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::openFormAccounts()
{
if(formAccounts == 0)
{
formAccounts = new FormAccounts();
ui->tabWidget->addTab(formAccounts, "Accounts");}
}
void MainWindow::listWidgetCurrentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{switch(ui->listWidget->row(current)) { case 1: openFormAccounts(); break; }
}
void MainWindow::removeTab(int index)
{QString classname = ui->tabWidget->widget(index)->metaObject()->className(); ui->tabWidget->removeTab(index); if (classname == "FormAccounts") { delete formAccounts; formAccounts = 0; } ui->listWidget->clearSelection(); ui->listWidget->setCurrentRow(-1);
}
@I think the mainwidow.ui is not necessary. There are the controls and the connections between slots an signals.
A lot of thanks,
Alex B.