我有一个程序,该程序在主窗口中打开一个带有用户列表和2个按钮的窗口,当按下一个按钮时,它将关闭/隐藏主窗口,并打开一个新窗口(在本例中为MarkdownEditorUi类)。
但是,当我执行.show()命令时,不会出现新窗口,并且我也不知道为什么,将int设置为主窗口(打开主窗口有效),但是在主窗口中打开第二个窗口无效。
我在方法中执行:FirstCplusPlusQt5Program :: buttonMarkdownAction()
CreateEditMarkdownNoteUi markdownEditor;
markdownEditor.show(); //显示降价编辑器窗口
this-> hide();
但是markdownEditor不会/打开显示新窗口。而且我在调试中知道代码已执行。
难道我做错了什么?它基于Qt Creator中创建的Windows。我应该工作。
另外:当在主窗口中调用新窗口时,它可以工作,并且代码完全相同。我不明白为什么它在主窗口中不起作用,而在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();
}
主视窗
#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.");
}
}
新视窗开启
该窗口在主要执行时打开,并且代码与预览代码完全相同
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?)。
谢谢您的提前帮助
最佳答案
你用:ui->setupUi(this);
如果要在新窗口中设置用户界面设计,则this
应该是此新窗口。请参阅https://stackoverflow.com/a/12182145/4599792。
但是,我怀疑在同一应用程序中是否具有两个QMainWindow类型的窗口是否明智。因此,让您的用户界面设计在QWidget窗口中运行,而不是在QMainWindow窗口中运行。