-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example running a dialog before the main window (#407)
* Example running a dialog before the main window Signed-off-by: Louise Poubel <louise@openrobotics.org> * Revert FIXMEs Signed-off-by: Louise Poubel <louise@openrobotics.org>
- Loading branch information
Showing
8 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR) | ||
|
||
project(gz-gui-start-dialog) | ||
|
||
if(POLICY CMP0100) | ||
cmake_policy(SET CMP0100 NEW) | ||
endif() | ||
|
||
set(CMAKE_AUTOMOC ON) | ||
|
||
find_package(ignition-gui3 REQUIRED) | ||
set(GZ_GUI_VER ${ignition-gui3_VERSION_MAJOR}) | ||
|
||
set(EXEC_NAME "start_dialog") | ||
|
||
QT5_ADD_RESOURCES(resources_RCC ${EXEC_NAME}.qrc) | ||
|
||
add_executable(${EXEC_NAME} | ||
${EXEC_NAME}.cc | ||
${resources_RCC} | ||
) | ||
target_link_libraries(${EXEC_NAME} | ||
ignition-gui${GZ_GUI_VER}::ignition-gui${GZ_GUI_VER} | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Example for how to run a start dialog before the main window. | ||
|
||
## Build | ||
|
||
cd <this directory> | ||
mkdir build | ||
cd build | ||
cmake .. | ||
make | ||
|
||
## Run | ||
|
||
cd <this directory>/build | ||
./start_dialog | ||
|
||
First the dialog shows up, and after that's closed, the main window shows up. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (C) 2022 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
#include <ignition/gui/qt.h> | ||
#include <ignition/gui/Application.hh> | ||
#include <ignition/gui/Dialog.hh> | ||
#include <ignition/gui/MainWindow.hh> | ||
|
||
////////////////////////////////////////////////// | ||
int main(int _argc, char **_argv) | ||
{ | ||
// Increase verboosity so we see all messages | ||
ignition::common::Console::SetVerbosity(4); | ||
|
||
// Create app | ||
ignition::gui::Application app(_argc, _argv, ignition::gui::WindowType::kDialog); | ||
|
||
igndbg << "Open dialog" << std::endl; | ||
|
||
// Add and display a dialog | ||
auto dialog = new ignition::gui::Dialog(); | ||
dialog->QuickWindow(); | ||
|
||
std::string qmlFile(":start_dialog/start_dialog.qml"); | ||
if (!QFile(QString::fromStdString(qmlFile)).exists()) | ||
{ | ||
ignerr << "Can't find [" << qmlFile | ||
<< "]. Are you sure it was added to the .qrc file?" << std::endl; | ||
return -1; | ||
} | ||
|
||
QQmlComponent dialogComponent(ignition::gui::App()->Engine(), | ||
QString(QString::fromStdString(qmlFile))); | ||
if (dialogComponent.isError()) | ||
{ | ||
std::stringstream errors; | ||
errors << "Failed to instantiate QML file [" << qmlFile << "]." | ||
<< std::endl; | ||
for (auto error : dialogComponent.errors()) | ||
{ | ||
errors << "* " << error.toString().toStdString() << std::endl; | ||
} | ||
ignerr << errors.str(); | ||
return -1; | ||
} | ||
|
||
auto dialogItem = qobject_cast<QQuickItem *>(dialogComponent.create()); | ||
if (!dialogItem) | ||
{ | ||
ignerr << "Failed to instantiate QML file [" << qmlFile << "]." << std::endl | ||
<< "Are you sure the file is valid QML? " | ||
<< "You can check with the `qmlscene` tool" << std::endl; | ||
return -1; | ||
} | ||
|
||
dialogItem->setParentItem(dialog->RootItem()); | ||
|
||
// Execute start dialog | ||
app.exec(); | ||
|
||
// After dialog is shut, display the main window | ||
igndbg << "Dialog closed, open main window" << std::endl; | ||
|
||
// Create main window | ||
app.CreateMainWindow(); | ||
|
||
// Run main window | ||
app.exec(); | ||
|
||
igndbg << "Main window closed" << std::endl; | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (C) 2022 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
import QtQuick 2.0 | ||
import QtQuick.Controls 2.0 | ||
Rectangle { | ||
color: "green" | ||
anchors.fill: parent | ||
Text { | ||
text: qsTr("Start\ndialog!") | ||
font.pointSize: 30 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<!DOCTYPE RCC><RCC version="1.0"> | ||
<qresource prefix="start_dialog/"> | ||
<file>start_dialog.qml</file> | ||
</qresource> | ||
</RCC> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters