Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // .h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QtGui>
- #include <QtCore>
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget * parent = 0);
- private slots:
- void createTab();
- void tabClose(int i);
- void openFile();
- private:
- void initMenu();
- void initWidget();
- QTabWidget * tab_;
- QMap<int, QWidget*> num_for_tab;
- QMap<QWidget*, QTextEdit*> textedit_for_tab;
- QMap<QWidget*, QString> file_for_tab;
- };
- #endif // MAINWINDOW_H
- // .cpp
- #include "MainWindow.h"
- MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent)
- {
- initMenu();
- initWidget();
- }
- void MainWindow::initWidget()
- {
- setWindowTitle("Theme Creator[*]");
- resize(640, 480);
- tab_ = new QTabWidget(this);
- tab_->setFocus();
- tab_->setTabsClosable(true);
- setCentralWidget(tab_);
- connect(tab_, SIGNAL(tabCloseRequested(int)), this, SLOT(tabClose(int)));
- }
- void MainWindow::initMenu()
- {
- QMenu * menuFile = menuBar()->addMenu("&Fichier");
- QAction * menuFileNew = menuFile->addAction("&Nouveau");
- connect(menuFileNew, SIGNAL(triggered()),
- this, SLOT(createTab()));
- QAction * menuFileOpen = menuFile->addAction("&Ouvrir");
- connect(menuFileOpen, SIGNAL(triggered()),
- this, SLOT(openFile()));
- QAction * menuFileSave = menuFile->addAction("Enregi&strer");
- QToolBar * toolBarFichier = addToolBar("Fichier");
- toolBarFichier->setMovable(false);
- toolBarFichier->addAction(menuFileNew);
- toolBarFichier->addAction(menuFileOpen);
- toolBarFichier->addAction(menuFileSave);
- }
- void MainWindow::createTab()
- {
- QWidget * page = new QWidget(this);
- QVBoxLayout * layout = new QVBoxLayout(page);
- layout->setContentsMargins(0, 0, 0, 0);
- page->setLayout(layout);
- QTextEdit * txtContenu = new QTextEdit(page);
- layout->addWidget(txtContenu);
- textedit_for_tab.insert(page, txtContenu);
- file_for_tab.insert(page, "");
- int compteur = 1;
- while(num_for_tab.keys().contains(compteur))
- {
- ++compteur;
- }
- num_for_tab.insert(compteur, page);
- txtContenu->setPlainText(QString::fromUtf8("n° : %1").arg(compteur));
- tab_->addTab(page, QString("Document sans nom %1").arg(compteur));
- //connect(txtContenu->document(), SIGNAL(modificationChanged(bool)),
- // this, SLOT(setWindowModified(bool)));
- tab_->setCurrentWidget(page);
- }
- void MainWindow::tabClose(int index)
- {
- QWidget * w = tab_->widget(index);
- if (w != NULL)
- {
- tab_->removeTab(index);
- int compteur = num_for_tab.key(w);
- num_for_tab.remove(compteur);
- QTextEdit * editor = textedit_for_tab.value(w);
- textedit_for_tab.remove(editor);
- w->deleteLater();
- }
- }
- void MainWindow::openFile()
- {
- QString filename = QFileDialog::getOpenFileName(this,tr("Open file"));
- if (!filename.isEmpty())
- {
- QFile file(filename);
- createTab();
- QWidget * current_tab = tab_->currentWidget();
- QTextEdit * txt_edit = textedit_for_tab.value(current_tab);
- int index = tab_->currentIndex();
- tab_->setTabText(index, filename.mid(filename.lastIndexOf("/") +1));
- if (txt_edit != NULL)
- {
- if (file.exists())
- {
- if (file.open(QIODevice::ReadOnly))
- {
- txt_edit->setText(file.readAll());
- file_for_tab.insert(current_tab, filename);
- file.close();
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment