PDA

View Full Version : Add menubar to QDialog or convert QDialog to QMainWindow ?



giblit
25th March 2013, 23:03
I made my program and declared it(the c++ file I am executing in the main c++ file) as a class public QDialog like this
#include <QDialog>
class DateListDialog: public QDialog {
Q_OBJECT
public:DateListDialog();
} but I want to add a menubar and I am not sure how if it is a QDialog so I wish to turn the QDialog into a QMainWindow I tried changing the code on the header to:
#include <QMainWindow>
class DateListDialog: public QMainWindow {
Q_OBJECT
public:DateListDialog();
}
but then when I do that and add the menubar all it shows it the menubar and not all the other gui items so basically its just an empty QMainWindow

the code I used to add the gui items is this:

#include <QtGui>
#include "datelistdialog.h"
#include "listwidgetdialog.h"
#include <sstream>
#include <iostream>
using namespace std;

DateListDialog::DateListDialog()
{

QStringList dates, foods, totalCalories, totalProteins, totalCarbs, totalFats, totalSugars, totalSodiums, totalFibers;
QStringList labels;
labels << tr("Dates") << tr("Foods") << tr("Total Calories") << tr("Total Proteins") << tr("Total Carbs") << tr("Total Fats") << tr("Total Sugars") << tr("Total Sodiums") << tr("Total Fibers");

treeWidget = new QTreeWidget;
treeWidget->setColumnCount(9);
treeWidget->setHeaderLabels(labels);


QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(treeWidget);
setLayout(mainLayout);


QFile file(QDir::homePath() + "/caloriecounter.txt");
if (file.open(QIODevice::ReadOnly)){
QTextStream input(&file);
input.setCodec("UTF-8");
while(!input.atEnd()){
//dates << input.readLine();
foods << input.readLine();
totalCalories << input.readLine();
totalProteins << input.readLine();
totalCarbs << input.readLine();
totalFats << input.readLine();
totalSugars << input.readLine();
totalSodiums << input.readLine();
totalFibers << input.readLine();
input.readLine();
}
}


for (int i = 0; i < foods.size(); ++i) {
QTreeWidgetItem* item = new QTreeWidgetItem;
//item->setText(0, dates[i]);
item->setText(1, foods[i]);
item->setText(2, totalCalories[i]);
item->setText(3, totalProteins[i]);
item->setText(4, totalCarbs[i]);
item->setText(5, totalFats[i]);
item->setText(6, totalSugars[i]);
item->setText(7,totalSodiums[i]);
item->setText(8, totalFibers[i]);
treeWidget->addTopLevelItem(item);

}
//createActions();
//createMenus();
}


Any suggestions would be greatly appreciated.
Thanks!

giblit
26th March 2013, 02:04
EDIT::: I think I figured a way how to do it but I will need a little bit of help with it I cannot find anything on the internet on how to do it.
What I think will work is to set the QDialog from the datelistdialog.cpp file as the main widget of another file called MainWindow which is a mainwindow
I tried doing this for the header:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{

public:
MainWindow();
};

#endif // MAINWINDOW_H

and this for the c++ file:

#include "mainwindow.h"
#include "datelistdialog.h"

MainWindow::MainWindow()
{
DateListDialog w;
setCentralWidget(w);
}


unfortunately it did not work =/
so I tried this for the c++

#include "mainwindow.h"
#include "datelistdialog.h"

MainWindow::MainWindow()
{
DateListDialog w;
w.exec();
}

it opens the QDialog but it does not set it as the centralWidget of the MainWindow tho =/

EDIT!:!:!:!:!:!:
The reason it was not working for me was after i turned it to a MainWindow I forgot the "setCentralWidget" because you do not need that on QDialogs.