Anfängerfrage: Die Menüleiste wird nicht angezeigt



  • Hallo,
    ich dachte mir es wäre eine gute Idee, das in der Uni vermittelte Wissen auch graphisch darstellen zu können und habe jetzt angefangen mir QT anzusehen.
    Der folgende Quellcode:

    hpp:

    #ifndef quit2_hpp
    #define quit2_hpp
    
    #include <QMainWindow>
    #include <QMenu>
    #include <QAction>
    #include <QTextEdit>
    
    class Notepad : public QMainWindow
    {
        Q_OBJECT
    private:
        QTextEdit* textEdit;
        QAction* openAction;
        QAction* saveAction;
        QAction* exitAction;
        QMenu* fileMenu;
        
    public:
        Notepad();
        
    private slots:
        void open();
        void save();
        void exit();
    };
    
    
    #endif
    
    

    .cpp

    #include "quitHeader.hpp"
    #include <QMenuBar>
    #include <QFileDialog>
    #include <QMessageBox>
    #include <QTextStream>
    #include <QApplication>
    
    Notepad :: Notepad()
    {
        openAction = new QAction(tr("open"), this);
        saveAction = new QAction(tr("save"), this);
        exitAction = new QAction(tr("exit"), this);
        
        connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
        connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
        connect(exitAction, SIGNAL(triggered()), this, SLOT(quit()));
        
        fileMenu = menuBar() -> addMenu(tr("file"));
        fileMenu->addAction(openAction);
        fileMenu->addAction(saveAction);
        fileMenu->addAction(exitAction);
        
        textEdit = new QTextEdit;
        setCentralWidget(textEdit);
        
        setWindowTitle(tr("Notepad"));
    }
    
    void Notepad :: open()
    {
        QString fileName = QFileDialog::getOpenFileName(this, tr("openfiles"), "", tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));
        
        if(fileName!="")
        {
            QFile file(fileName);
        
            if(!file.open(QIODevice::ReadOnly))
            {
                QMessageBox::critical(this, tr("error"), tr("could not open file"));
                return;
            }
        
            QString contents = file.readAll().constData();
            textEdit->setPlainText(contents);
            file.close();
        }
    
    }
    
    
    void Notepad :: save()
    {
        QString fileName = QFileDialog::getSaveFileName(this, tr("savefiles"), "", tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));
        
        if(fileName!="")
        {
            QFile file(fileName);
            if(!file.open(QIODevice::WriteOnly))
            {
                QMessageBox::critical(this, tr("error"), tr("could not save file"));
                return;
            } else {
                QTextStream stream(&file);
                stream << textEdit->toPlainText();
                stream.flush();
                file.close();
            }
        }
    }
            
    void Notepad :: exit()
    {
        QMessageBox messageBox;
        
        messageBox.setWindowTitle("Notepad");
        messageBox.setText(tr("Do you realy wanna quit?"));
        messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        messageBox.setDefaultButton(QMessageBox::No);
        if(messageBox.exec() == QMessageBox::Yes)
            qApp->quit();
    }
    

    main:

    #include "quitHeader.hpp"
    #include <QApplication>
    
    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
        Notepad notepad;
        notepad.show();
        
        return app.exec();
    }
    

    produziert bei mir quasi nichts außer das Notizfeld. Was habe ich im Konstruktor falsch gemacht, dass mir die Menüleiste nicht angezeigt wird?
    Der Fehler dürfte wohl an der Zeile 18:
    fileMenu = menuBar() -> addMenu(tr("file"));
    liegen.

    Falls sich wer die Mühe macht, vielen Dank im voraus 🙂



  • Ich habe das Programm auf Linux mit Qt 5.12 übersetzt. Ein Menü ist vorhanden und es tut auch etwas. Der einzige Fehler: du hast exit statt quit verbunden.



  • Danke für den Hinweis mit quit()/exit().

    Bei mir sieht es nur so aus:
    https://www.bilder-upload.eu/bild-378c66-1551564211.png.html

    compiliert mit
    1: qmake -project
    2: in der .pro Datei QT += widgets eingefügt
    3: qmake
    4: make

    Hinweis:
    »qtcreator« ist bereits die neuste Version (3.5.1+dfsg-2ubuntu2).



  • Hast du Ubuntu? Evtl. hilft dann QMenuBar is not displayed in form in Linux?

    Siehst du das Menu denn, wenn du textEdit nicht anzeigst (bzw. die Reihenfolge vertauschst)?



  • @TristanS sagte in Anfängerfrage: Die Menüleiste wird nicht angezeigt:

    ich dachte mir es wäre eine gute Idee, das in der Uni vermittelte Wissen

    Hahaha uni <-> wissen. lol.



  • @Th69 sagte in Anfängerfrage: Die Menüleiste wird nicht angezeigt:

    Hast du Ubuntu? Evtl. hilft dann QMenuBar is not displayed in form in Linux?

    Siehst du das Menu denn, wenn du textEdit nicht anzeigst (bzw. die Reihenfolge vertauschst)?

    Vielen Dank, das hat geholfen. Die Menüleiste wird korrekt angezeigt, jedoch ist sie nicht ans Fenster gebunden und ist immer oben in der Leiste. Mal sehen, ob ich eine Möglichkeit finde, das zu umgehen. 🙂



  • was soll das du verdammter downvoter? sobald's mich kratzt sag ich dir bescheid, ok?



  • Ich hab dich mal wieder upgevoted, find die Antwort auch nicht verkehrt 😉
    Hat man früher nicht im Tooltip gesehen, wer alles abgestimmt hat?



  • @Mechanics bei Downvotes sieht man nicht welcher Arsch User es war


Anmelden zum Antworten