Qt无法打开新窗口

时间:2017-07-22 11:23:49

标签: c++ qt user-interface

我有一个程序在主要打开一个带有用户列表和2个按钮的窗口,当按下一个按钮时,它应该关闭/隐藏主窗口,并打开一个新窗口(在这种情况下是MarkdownEditorUi类)。

但是当我执行.show()命令时,新窗口没有出现,我不知道为什么,在主窗口(打开主窗口工作),但在主窗口打开第二个窗口不工作

  • 我在方法中做了:FirstCplusPlusQt5Program :: buttonMarkdownAction()

    CreateEditMarkdownNoteUi markdownEditor;   markdownEditor.show(); //显示降价编辑器窗口   这 - >隐藏();

但是markdownEditor不打开/打开显示一个新窗口。我在调试中知道代码已执行。

我做错了吗?它基于在Qt Creator中创建的窗口。我应该工作。

另外:当在main中调用新窗口时,它可以工作,并且代码完全相同。我不明白它为什么不在主窗口中工作,但在void main()中工作;

代码

主要

int main(int argc, char *argv[]){

    RuntimeContainer r(20);
    r.insertLog("Start main","Starting everything ", Log::PROGRAM_STARTUP);

    cout << "Fist log inserted in main memory: \n> Name--> " << r.getLogList().getLogByIndex(0).getName().toStdString() << " \n--> Description: "<< r.getLogList().getLogByIndex(0).getDescription().toStdString();


    QApplication app(argc, argv);
    FirstCplusPlusQt5Program w; 
    w.setRunTimeContainer(r);
    w.show();           //open the main window



    return app.exec();

}

主窗口

enter image description here

    #include "firstcplusplusqt5program.h"
#include "ui_firstcplusplusqt5program.h"

#include <QString>

#include "UC/CreateCategory/UI/createCategoryUi.h"



/**
 * @brief window constructor
 * 
 * @param parent p_parent:...
 */
FirstCplusPlusQt5Program::FirstCplusPlusQt5Program(QWidget *parent) : QMainWindow(parent), ui(new Ui::FirstCplusPlusQt5Program){
    ui->setupUi(this);


    //test();

    /**
     *  connection of the button when its pressed with the method "buttonMarkdownAction()"
     **/
    connect(ui->qPushButtonMarkdown, SIGNAL (clicked()),this,SLOT (buttonMarkdownAction()));
    connect(ui->qPushButtonPlantUml, SIGNAL (clicked()),this,SLOT (buttonPlantUmlAction()));

}

/**
 * @brief default destructor
 * 
 */
FirstCplusPlusQt5Program::~FirstCplusPlusQt5Program(){
    delete ui;
}




/**
 *  
 * @brief when button pressed opens markdown editor, is a private slot
 * @details markdown editor is a new window. Validates if user is selected
 */
void FirstCplusPlusQt5Program::buttonMarkdownAction(){
    if (ui->qListWidgetUserList->selectedItems().size() > 0){       //if user list have selected user
        //do things
        QString s = ui->qListWidgetUserList->selectedItems()[0]->text();   //gets selected user name

        if (s.contains(QRegExp(USER_NAME_REGEX_DEFAULT))){      //VALIDATE USER name, database may be currompted
            User userToLogOn = this->r.getUserList().findUser(s);

            cout << "User: " << userToLogOn.getName().toStdString() << " ==> Email " << userToLogOn.getEmail().toStdString() << endl; //FOR TESTING
            CreateEditMarkdownNoteUi markdownEditor;
            markdownEditor.show();          //show markdown editor window
            this->hide();       //hide current window
            cout << "Test";     //FOR TESTING
        }
    }else{
        //error, informs the user from error
        noUserSelected("No user selected!!","Please select user", "No save the notes in differents login locations is needed a user.");

    }
}

要打开的新窗口

  
    

这个窗口在主要执行时打开,并且该代码在预览代码中完全相同

  

enter image description here

CPP
#include "createeditmarkdownnoteui.h"
#include "ui_createeditmarkdownnoteui.h"
//#include <QWebEnginePage>

CreateEditMarkdownNoteUi::CreateEditMarkdownNoteUi(QWidget* parent)  : QMainWindow(parent),ui(new Ui::CreateEditMarkdownNoteUi){

    ui->setupUi(this);
}



CreateEditMarkdownNoteUi::~CreateEditMarkdownNoteUi(){
    delete ui;
}
。H
#ifndef CREATEEDITMARKDOWNNOTEUI_H
#define CREATEEDITMARKDOWNNOTEUI_H

#include <QMainWindow>

namespace Ui
{
    class CreateEditMarkdownNoteUi;
}

class CreateEditMarkdownNoteUi : public QMainWindow
{
    Q_OBJECT

public:

    explicit CreateEditMarkdownNoteUi(QWidget* parent = 0);

    ~CreateEditMarkdownNoteUi();

private:
    Ui::CreateEditMarkdownNoteUi* ui;
};

#endif // CREATEEDITMARKDOWNNOTEUI_H
的.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
    <class>CreateEditMarkdownNoteUi</class>
    <widget class="QMainWindow" name="CreateEditMarkdownNoteUi">
        <property name="geometry">
            <rect>
                <x>0</x>
                <y>0</y>
                <width>800</width>
                <height>600</height>
            </rect>
        </property>
        <property name="windowTitle">
            <string>CreateEditMarkdownNoteUi</string>
        </property>
        <widget class="QWidget" name="centralwidget"/>
        <widget class="QMenuBar" name="menubar">
            <property name="geometry">
                <rect>
                    <x>0</x>
                    <y>0</y>
                    <width>800</width>
                    <height>23</height>
                </rect>
            </property>
        </widget>
        <widget class="QStatusBar" name="statusbar"/>
    </widget>
    <resources/>
    <connections/>
</ui>

Cmake列表(类似于make或qmake,带有cpp文件列表和.ui来运行/编译)

cmake_minimum_required(VERSION 2.8.11)
project(FirstCplusPlusQt5Program)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

# Find the QtWidgets library
find_package(Qt5Widgets REQUIRED)


#
#
# @author : josemiguel443@gmail.com = add src file of .cpp of used/compiled 
#classes
#
#
#
set(

  # PROJECT DEPENDENCIES
  firstcplusplusqt5program_SRC
  src/main.cpp
  src/firstcplusplusqt5program.cpp

  # DOMAIN
  src/Domain/Category.cpp
  src/Domain/User/user.cpp
  src/Domain/note.cpp

  #DOMAIN LIST

  src/Domain/List/categorylist.cpp

  # LOG
  src/RuntimeLog/log.cpp
  src/RuntimeLog/loglist.cpp



  # DOMAIN LIST

  src/Domain/List/noteList.cpp
  src/Domain/List/userList.cpp

  # UTILS
  src/Utils/utils.cpp

   # UC
  src/UC/CreateCategory/UI/createCategoryUi.cpp
  src/UC/CreateNoteWithMarkdown/UI/createeditmarkdownnoteui.cpp          #note used yet

  # PERSISTENCE
  src/Persistence/InMemory/runtimecontainer.cpp

)

# Create code from a list of Qt designer ui files.
#set(CMAKE_AUTOUIC ON) # use this if you have CMake 3.x instead of the following
#
#
# @author : josemiguel443@gmail.com = add src file of .ui for QT
#
#
#
qt5_wrap_ui(firstcplusplusqt5program_SRC 
    src/firstcplusplusqt5program.ui
    src/UC/CreateCategory/UI/createCategoryUi.ui
    #src/UC/CreateNoteWithMarkdown/createeditmarkdownnoteui.ui          #note used yet
)






# Tell CMake to create the helloworld executable
add_executable(firstcplusplusqt5program ${firstcplusplusqt5program_SRC})

# Use the Widgets module from Qt 5.
target_link_libraries(firstcplusplusqt5program Qt5::Widgets)

# Install the executable
install(TARGETS firstcplusplusqt5program DESTINATION bin)

输出

user19User: user5 ==> Email user5@gmail.com
Test
*** Finished ***

我已经尝试过在堆栈溢出的另一个线程中给出的解决方案,但是我不明白为什么(How to open a new window from the main window in Qt?)。

感谢您提前获得帮助

2 个答案:

答案 0 :(得分:2)

问题是对象超出了范围。

main函数中,由于事件循环app.exec();,窗口打开并且仍然打开。它的阻止到达主函数范围的末尾,直到程序结束并且事件循环返回退出代码。

要解决此问题而不需要对代码进行太多更改,可以使用new创建类并解决范围问题,将父类设置为此类以启用自动删除或明确设置{ {1}}具有相同的目的。

另请注意,如果所有窗口都已关闭且setAttribute(Qt::WA_DeleteOnClose);整个应用程序都将关闭,那么必要时请调用QApplication::quitOnLastWindowClosed() == true;并使用QApplication::setQuitOnLastWindowClosed(false);显式退出应用程序,例如:

QApplication::quit();

答案 1 :(得分:0)

您使用: ui->setupUi(this);

如果您希望在新窗口中设置您的ui-design,this应该是这个新窗口。请参阅https://stackoverflow.com/a/12182145/4599792

我怀疑在同一个应用程序中是否智能或可能有两个 QMainWindow 类型的 windows 。因此,让您的ui-design在 QWidget 窗口中运行,但不能在 QMainWindow 窗口中运行。