Results 1 to 2 of 2

Thread: Add menubar to QDialog or convert QDialog to QMainWindow ?

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Add menubar to QDialog or convert QDialog to QMainWindow ?

    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
    Qt Code:
    1. #include <QDialog>
    2. class DateListDialog: public QDialog {
    3. Q_OBJECT
    4. public:DateListDialog();
    5. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. #include <QMainWindow>
    2. class DateListDialog: public QMainWindow {
    3. Q_OBJECT
    4. public:DateListDialog();
    5. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. #include <QtGui>
    2. #include "datelistdialog.h"
    3. #include "listwidgetdialog.h"
    4. #include <sstream>
    5. #include <iostream>
    6. using namespace std;
    7.  
    8. DateListDialog::DateListDialog()
    9. {
    10.  
    11. QStringList dates, foods, totalCalories, totalProteins, totalCarbs, totalFats, totalSugars, totalSodiums, totalFibers;
    12. QStringList labels;
    13. 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");
    14.  
    15. treeWidget = new QTreeWidget;
    16. treeWidget->setColumnCount(9);
    17. treeWidget->setHeaderLabels(labels);
    18.  
    19.  
    20. QVBoxLayout *mainLayout = new QVBoxLayout;
    21. mainLayout->addWidget(treeWidget);
    22. setLayout(mainLayout);
    23.  
    24.  
    25. QFile file(QDir::homePath() + "/caloriecounter.txt");
    26. if (file.open(QIODevice::ReadOnly)){
    27. QTextStream input(&file);
    28. input.setCodec("UTF-8");
    29. while(!input.atEnd()){
    30. //dates << input.readLine();
    31. foods << input.readLine();
    32. totalCalories << input.readLine();
    33. totalProteins << input.readLine();
    34. totalCarbs << input.readLine();
    35. totalFats << input.readLine();
    36. totalSugars << input.readLine();
    37. totalSodiums << input.readLine();
    38. totalFibers << input.readLine();
    39. input.readLine();
    40. }
    41. }
    42.  
    43.  
    44. for (int i = 0; i < foods.size(); ++i) {
    45. //item->setText(0, dates[i]);
    46. item->setText(1, foods[i]);
    47. item->setText(2, totalCalories[i]);
    48. item->setText(3, totalProteins[i]);
    49. item->setText(4, totalCarbs[i]);
    50. item->setText(5, totalFats[i]);
    51. item->setText(6, totalSugars[i]);
    52. item->setText(7,totalSodiums[i]);
    53. item->setText(8, totalFibers[i]);
    54. treeWidget->addTopLevelItem(item);
    55.  
    56. }
    57. //createActions();
    58. //createMenus();
    59. }
    To copy to clipboard, switch view to plain text mode 


    Any suggestions would be greatly appreciated.
    Thanks!

  2. #2
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: Add menubar to QDialog or convert QDialog to QMainWindow ?

    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:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include <QMainWindow>
    4. class MainWindow : public QMainWindow
    5. {
    6.  
    7. public:
    8. MainWindow();
    9. };
    10.  
    11. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    and this for the c++ file:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "datelistdialog.h"
    3.  
    4. MainWindow::MainWindow()
    5. {
    6. DateListDialog w;
    7. setCentralWidget(w);
    8. }
    To copy to clipboard, switch view to plain text mode 

    unfortunately it did not work =/
    so I tried this for the c++
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "datelistdialog.h"
    3.  
    4. MainWindow::MainWindow()
    5. {
    6. DateListDialog w;
    7. w.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by giblit; 26th March 2013 at 02:32.

Similar Threads

  1. Replies: 0
    Last Post: 22nd September 2011, 10:31
  2. Replies: 3
    Last Post: 22nd September 2011, 05:47
  3. How to Call a QDialog in QMainWindow
    By mcht_z in forum Newbie
    Replies: 2
    Last Post: 13th April 2011, 06:43
  4. manipulating qmainwindow widget through qdialog
    By xeroblast in forum Qt Programming
    Replies: 1
    Last Post: 16th December 2010, 07:17
  5. QMainWindow child of a QDialog
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2008, 07:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.