Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Application won't always start
Forum Updated to NodeBB v4.3 + New Features

Application won't always start

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 498 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tilz0R
    wrote on last edited by tilz0R
    #1

    I'm learning Qt for C++ (with Python I made pretty good progress) and I have notices that sometimes my window won't pop-up, logs in the window constructor won't get printed (qDebug()<<"message..." and similar type of messages).

    I'm communicating with my DLL to access serial port, so I though this may have an impact, but after removing it, I don't see difference. Still, out of 10 attempts to start .exe, Window will show maybe around 60% of the times.

    Some other info:

    • Windows 11
    • Qt version 6.5
    • CMake as build system, developed under vscode
    • Qt paths set accordingly
    • There is no error at all, application seems to start and do something in the background, but windows doesn't open

    If it's needed, below is main.c and mainwindow.cpp.

    #include "mainwindow.h"
    #include <QApplication>
    
    int
    main(int argc, char* argv[]) {
        QApplication a(argc, argv);
        
        //These 2 prints sometimes don't show in the console at all
        qDebug() << "Before MainWindow constructor";
        MainWindow w;
        qDebug() << "After MainWindow constructor";
        w.show();
        return a.exec();
    }
    

    mainwindow class constructor

    MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
        ui->setupUi(this);
    
        /* Create custom views */
        view_can_config = new ViewCanConfig();
        view_debug = new ViewDebug();
        view_fw_update = new ViewFwUpdate();
        view_logger = new ViewLogger();
        view_tx_msg_config = new ViewTxMsgConfig();
        views_list.append(view_can_config);
        views_list.append(view_debug);
        views_list.append(view_fw_update);
        views_list.append(view_logger);
        views_list.append(view_tx_msg_config);
        connect(view_tx_msg_config, ViewTxMsgConfig::onSendMessage, this, MainWindow::send_can_message);
        connect(view_logger, ViewLogger::onSendMessage, this, MainWindow::send_can_message);
    
        /* Connect debug messages */
        connect(view_can_config, ViewCanConfig::send_debug, view_debug, ViewDebug::add_debug_message);
        connect(view_debug, ViewDebug::send_debug, view_debug, ViewDebug::add_debug_message);
        connect(view_fw_update, ViewFwUpdate::send_debug, view_debug, ViewDebug::add_debug_message);
        connect(view_logger, ViewLogger::send_debug, view_debug, ViewDebug::add_debug_message);
        connect(view_tx_msg_config, ViewTxMsgConfig::send_debug, view_debug, ViewDebug::add_debug_message);
        connect(this, MainWindow::send_debug, view_debug, ViewDebug::add_debug_message);
    
        /* Add views to the layouts */
        ui->layout_tab_logger->addWidget(view_logger);
        ui->layout_tab_debug->addWidget(view_debug);
        ui->layout_tab_fdcan_config->addWidget(view_can_config);
        ui->layout_tab_tx_msg_config->addWidget(view_tx_msg_config);
        ui->layout_tab_fw_update->addWidget(view_fw_update);
    
        /* Add quick TX widget to the logger view using default configuration */
        view_logger->add_quick_tx_widget(view_tx_msg_config->create_new_tx_msg_config_window());
    
        /* Set status bar */
        statusbar = new StatusBar(this);
        ui->statusbar->addWidget(statusbar);
    
        /* Triggers */
        connect(ui->btn_com_port_refresh, &QPushButton::clicked, this, &MainWindow::load_com_ports);
        connect(ui->btn_com_port_connect_disconnect, &QPushButton::clicked, this, &MainWindow::toggle_connect_to_port);
    
        /* Get system com ports */
    #if 1
        load_com_ports();
    
        /* Reset values */
        ca_evt_read_ptr = 0;
        ca_evt_write_ptr = 0;
    
        /* Queue processing timer */
        timer_queue = new QTimer(this);
        timer_queue->setSingleShot(false);
        timer_queue->setInterval(50);
        connect(timer_queue, &QTimer::timeout, this, &MainWindow::timer_ca_queue_check_expired);
        timer_queue->start();
    
        emit send_debug(CaUtils::DebugLevel::Info, "APP", "Application running");
    
        /* Initialize CANanalyzer interface */
        if (ca_init((ca_evt_fn)&prv_ca_evt_fn)) {
            emit send_debug(CaUtils::DebugLevel::Info, "DLL", "App ready to use");
        }
    #endif
    }
    

    Am I missing something? What could be common reasons that app wouldn't always start properly?

    Axel SpoerlA 2 Replies Last reply
    0
    • T Offline
      T Offline
      tilz0R
      wrote on last edited by
      #7

      Found the problem. It was uninitialized variable in another class that was constructed in the constructor of MainWindow, causing for loop to go to to 2^32 or other random number.

      Sorry for taking your time for this.

      The Before MainWindow... was NOT always printed. It may have to do with flushing of the data in the system.

      I will continue monitoring but it looks good for the moment.

      1 Reply Last reply
      0
      • T tilz0R

        I'm learning Qt for C++ (with Python I made pretty good progress) and I have notices that sometimes my window won't pop-up, logs in the window constructor won't get printed (qDebug()<<"message..." and similar type of messages).

        I'm communicating with my DLL to access serial port, so I though this may have an impact, but after removing it, I don't see difference. Still, out of 10 attempts to start .exe, Window will show maybe around 60% of the times.

        Some other info:

        • Windows 11
        • Qt version 6.5
        • CMake as build system, developed under vscode
        • Qt paths set accordingly
        • There is no error at all, application seems to start and do something in the background, but windows doesn't open

        If it's needed, below is main.c and mainwindow.cpp.

        #include "mainwindow.h"
        #include <QApplication>
        
        int
        main(int argc, char* argv[]) {
            QApplication a(argc, argv);
            
            //These 2 prints sometimes don't show in the console at all
            qDebug() << "Before MainWindow constructor";
            MainWindow w;
            qDebug() << "After MainWindow constructor";
            w.show();
            return a.exec();
        }
        

        mainwindow class constructor

        MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
            ui->setupUi(this);
        
            /* Create custom views */
            view_can_config = new ViewCanConfig();
            view_debug = new ViewDebug();
            view_fw_update = new ViewFwUpdate();
            view_logger = new ViewLogger();
            view_tx_msg_config = new ViewTxMsgConfig();
            views_list.append(view_can_config);
            views_list.append(view_debug);
            views_list.append(view_fw_update);
            views_list.append(view_logger);
            views_list.append(view_tx_msg_config);
            connect(view_tx_msg_config, ViewTxMsgConfig::onSendMessage, this, MainWindow::send_can_message);
            connect(view_logger, ViewLogger::onSendMessage, this, MainWindow::send_can_message);
        
            /* Connect debug messages */
            connect(view_can_config, ViewCanConfig::send_debug, view_debug, ViewDebug::add_debug_message);
            connect(view_debug, ViewDebug::send_debug, view_debug, ViewDebug::add_debug_message);
            connect(view_fw_update, ViewFwUpdate::send_debug, view_debug, ViewDebug::add_debug_message);
            connect(view_logger, ViewLogger::send_debug, view_debug, ViewDebug::add_debug_message);
            connect(view_tx_msg_config, ViewTxMsgConfig::send_debug, view_debug, ViewDebug::add_debug_message);
            connect(this, MainWindow::send_debug, view_debug, ViewDebug::add_debug_message);
        
            /* Add views to the layouts */
            ui->layout_tab_logger->addWidget(view_logger);
            ui->layout_tab_debug->addWidget(view_debug);
            ui->layout_tab_fdcan_config->addWidget(view_can_config);
            ui->layout_tab_tx_msg_config->addWidget(view_tx_msg_config);
            ui->layout_tab_fw_update->addWidget(view_fw_update);
        
            /* Add quick TX widget to the logger view using default configuration */
            view_logger->add_quick_tx_widget(view_tx_msg_config->create_new_tx_msg_config_window());
        
            /* Set status bar */
            statusbar = new StatusBar(this);
            ui->statusbar->addWidget(statusbar);
        
            /* Triggers */
            connect(ui->btn_com_port_refresh, &QPushButton::clicked, this, &MainWindow::load_com_ports);
            connect(ui->btn_com_port_connect_disconnect, &QPushButton::clicked, this, &MainWindow::toggle_connect_to_port);
        
            /* Get system com ports */
        #if 1
            load_com_ports();
        
            /* Reset values */
            ca_evt_read_ptr = 0;
            ca_evt_write_ptr = 0;
        
            /* Queue processing timer */
            timer_queue = new QTimer(this);
            timer_queue->setSingleShot(false);
            timer_queue->setInterval(50);
            connect(timer_queue, &QTimer::timeout, this, &MainWindow::timer_ca_queue_check_expired);
            timer_queue->start();
        
            emit send_debug(CaUtils::DebugLevel::Info, "APP", "Application running");
        
            /* Initialize CANanalyzer interface */
            if (ca_init((ca_evt_fn)&prv_ca_evt_fn)) {
                emit send_debug(CaUtils::DebugLevel::Info, "DLL", "App ready to use");
            }
        #endif
        }
        

        Am I missing something? What could be common reasons that app wouldn't always start properly?

        Axel SpoerlA Offline
        Axel SpoerlA Offline
        Axel Spoerl
        Moderators
        wrote on last edited by
        #2

        @tilz0R
        How is the application compiled and how is it launched?
        Are you always launching the debug build?

        To narrow the issue down: Use qInfo()instead of qDebug()and start your executable from the command line. Watch out for error messages on startup.

        Software Engineer
        The Qt Company, Oslo

        T 1 Reply Last reply
        0
        • Axel SpoerlA Axel Spoerl

          @tilz0R
          How is the application compiled and how is it launched?
          Are you always launching the debug build?

          To narrow the issue down: Use qInfo()instead of qDebug()and start your executable from the command line. Watch out for error messages on startup.

          T Offline
          T Offline
          tilz0R
          wrote on last edited by
          #3

          @Axel-Spoerl

          Compiled with CMake:

          # configure
          cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_TOOLCHAIN_FILE=D:/.../cmakefile/x86_64-w64-mingw32-gcc.cmake -SD:/.../gui_cpp -BD:/.../build/Win64-Debug -G Ninja
          # Build
          cmake --build path/to/dir
          

          Toolchain file

          set(CMAKE_SYSTEM_NAME               Windows)
          
          # Some default GCC settings
          set(CMAKE_C_COMPILER                x86_64-w64-mingw32-gcc)
          set(CMAKE_CXX_COMPILER              x86_64-w64-mingw32-g++)
          
          set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
          

          Toolchain

          x86_64-w64-mingw32-gcc --version
          x86_64-w64-mingw32-gcc (Rev6, Built by MSYS2 project) 12.2.0
          Copyright (C) 2022 Free Software Foundation, Inc.
          This is free software; see the source for copying conditions.  There is NO
          warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
          

          CMakeLists file

          cmake_minimum_required(VERSION 3.22)
          
          project("CANanalyzer" VERSION 0.1 LANGUAGES C CXX)
          
          set(CMAKE_AUTOMOC ON)
          set(CMAKE_AUTOUIC ON)
          set(CMAKE_AUTORCC ON)
          
          set(CMAKE_CXX_STANDARD          17)
          set(CMAKE_CXX_STANDARD_REQUIRED ON)
          #set(CMAKE_PREFIX_PATH "c:/Qt6.5.0/6.5.0/mingw_64/")
          
          find_package(QT NAMES Qt6 REQUIRED COMPONENTS Widgets)
          find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core)
          find_package(QT NAMES Qt6 REQUIRED COMPONENTS SerialPort)
          find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
          find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
          find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS SerialPort)
          
          # Define include paths
          set(PROJECT_INCLUDES
              ${CMAKE_CURRENT_LIST_DIR}/
              ${CMAKE_CURRENT_LIST_DIR}/include
              ${CMAKE_CURRENT_LIST_DIR}/models
              ${CMAKE_CURRENT_LIST_DIR}/widgetss
              ${CMAKE_CURRENT_LIST_DIR}/views
              ${CMAKE_CURRENT_LIST_DIR}/../common/include
              ${CMAKE_CURRENT_LIST_DIR}/../common/proto
              ${CMAKE_CURRENT_LIST_DIR}/../libs/nanopb
              ${CMAKE_CURRENT_LIST_DIR}/../libs/mbedtls/include
          )
          
          # Define project sources
          set(PROJECT_SOURCES
              ${CMAKE_CURRENT_LIST_DIR}/main.cpp
              ${CMAKE_CURRENT_LIST_DIR}/mainwindow.cpp
          
              # Widgets & views
              ${CMAKE_CURRENT_LIST_DIR}/widgets/tx_msg_config_entry_widget.cpp
              ${CMAKE_CURRENT_LIST_DIR}/widgets/can_clock_config_entry_widget.cpp
              ${CMAKE_CURRENT_LIST_DIR}/widgets/can_clock_config_entry_widget_timepar.cpp
              ${CMAKE_CURRENT_LIST_DIR}/widgets/statusbar.cpp
              ${CMAKE_CURRENT_LIST_DIR}/views/view_can_config.cpp
              ${CMAKE_CURRENT_LIST_DIR}/views/view_debug.cpp
              ${CMAKE_CURRENT_LIST_DIR}/views/view_logger.cpp
              ${CMAKE_CURRENT_LIST_DIR}/views/view_fw_update.cpp
              ${CMAKE_CURRENT_LIST_DIR}/views/view_tx_msg_config.cpp
              
              # Models
              ${CMAKE_CURRENT_LIST_DIR}/models/canloggerdatamodel.cpp
          
              # UI files
              ${CMAKE_CURRENT_LIST_DIR}/mainwindow.ui
              ${CMAKE_CURRENT_LIST_DIR}/widgets/can_clock_config_entry_widget.ui
              ${CMAKE_CURRENT_LIST_DIR}/widgets/tx_msg_config_entry_widget.ui
              ${CMAKE_CURRENT_LIST_DIR}/views/view_can_config.ui
              ${CMAKE_CURRENT_LIST_DIR}/views/view_logger.ui
              ${CMAKE_CURRENT_LIST_DIR}/views/view_fw_update.ui
              ${CMAKE_CURRENT_LIST_DIR}/views/view_debug.ui
              ${CMAKE_CURRENT_LIST_DIR}/views/view_tx_msg_config.ui
          
              # Missing this, MOC won't run for custom header files !!
              ${CMAKE_CURRENT_LIST_DIR}/views/custom_tab_widget_base.h
          )
          
          # Add mbedTLS
          file(GLOB MBEDTLS_FILES ${CMAKE_CURRENT_LIST_DIR}/../libs/mbedtls/library/*.c)
          set(PROJECT_SOURCES ${PROJECT_SOURCES} ${MBEDTLS_FILES})
          
          # Create executable file
          qt_add_executable(${CMAKE_PROJECT_NAME}
              MANUAL_FINALIZATION
              ${PROJECT_SOURCES}
          )
          
          # Set application firmware version -> read from the file directly
          include(../cmake/version.cmake)
          
          # Add to build
          target_compile_definitions(${CMAKE_PROJECT_NAME} PUBLIC
              # Version setup
              "CA_FW_VERSION_MAJOR=${CA_FW_VERSION_MAJOR}"
              "CA_FW_VERSION_MINOR=${CA_FW_VERSION_MINOR}"
              "CA_FW_VERSION_PATCH=${CA_FW_VERSION_PATCH}"
              "CA_FW_VERSION_TYPE=${CA_FW_VERSION_TYPE}"
              "CA_FW_VERSION_REV=${CA_FW_VERSION_REV}"
          
              # Nano protobuf
              "PB_FIELD_32BIT"
          )
          
          # Set include paths
          target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${PROJECT_INCLUDES})
          
          # Link Qt libraries
          target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
              Qt${QT_VERSION_MAJOR}::Widgets
              Qt${QT_VERSION_MAJOR}::Core
              Qt${QT_VERSION_MAJOR}::SerialPort
          )
          
          # Add cananalyzer library
          target_link_directories(${PROJECT_NAME} PUBLIC build/Win64-Debug)
          target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE libCANanalyzer)
          
          set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
              MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
              MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
              MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
              MACOSX_BUNDLE TRUE
              WIN32_EXECUTABLE OFF
          )
          
          install(TARGETS ${CMAKE_PROJECT_NAME}
              BUNDLE DESTINATION .
              LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
          
          if(QT_VERSION_MAJOR EQUAL 6)
              qt_finalize_executable(${CMAKE_PROJECT_NAME})
          endif()
          

          Start command

          gui_cpp\build\Win64-Debug>.\CANanalyzer.exe
          Before MainWindow constructor
          

          For instance, I got Before MainWindow constructor but then no After MainWindow message. No log messages are returned.

          Christian EhrlicherC Axel SpoerlA 2 Replies Last reply
          0
          • T tilz0R

            @Axel-Spoerl

            Compiled with CMake:

            # configure
            cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_TOOLCHAIN_FILE=D:/.../cmakefile/x86_64-w64-mingw32-gcc.cmake -SD:/.../gui_cpp -BD:/.../build/Win64-Debug -G Ninja
            # Build
            cmake --build path/to/dir
            

            Toolchain file

            set(CMAKE_SYSTEM_NAME               Windows)
            
            # Some default GCC settings
            set(CMAKE_C_COMPILER                x86_64-w64-mingw32-gcc)
            set(CMAKE_CXX_COMPILER              x86_64-w64-mingw32-g++)
            
            set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
            

            Toolchain

            x86_64-w64-mingw32-gcc --version
            x86_64-w64-mingw32-gcc (Rev6, Built by MSYS2 project) 12.2.0
            Copyright (C) 2022 Free Software Foundation, Inc.
            This is free software; see the source for copying conditions.  There is NO
            warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
            

            CMakeLists file

            cmake_minimum_required(VERSION 3.22)
            
            project("CANanalyzer" VERSION 0.1 LANGUAGES C CXX)
            
            set(CMAKE_AUTOMOC ON)
            set(CMAKE_AUTOUIC ON)
            set(CMAKE_AUTORCC ON)
            
            set(CMAKE_CXX_STANDARD          17)
            set(CMAKE_CXX_STANDARD_REQUIRED ON)
            #set(CMAKE_PREFIX_PATH "c:/Qt6.5.0/6.5.0/mingw_64/")
            
            find_package(QT NAMES Qt6 REQUIRED COMPONENTS Widgets)
            find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core)
            find_package(QT NAMES Qt6 REQUIRED COMPONENTS SerialPort)
            find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
            find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
            find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS SerialPort)
            
            # Define include paths
            set(PROJECT_INCLUDES
                ${CMAKE_CURRENT_LIST_DIR}/
                ${CMAKE_CURRENT_LIST_DIR}/include
                ${CMAKE_CURRENT_LIST_DIR}/models
                ${CMAKE_CURRENT_LIST_DIR}/widgetss
                ${CMAKE_CURRENT_LIST_DIR}/views
                ${CMAKE_CURRENT_LIST_DIR}/../common/include
                ${CMAKE_CURRENT_LIST_DIR}/../common/proto
                ${CMAKE_CURRENT_LIST_DIR}/../libs/nanopb
                ${CMAKE_CURRENT_LIST_DIR}/../libs/mbedtls/include
            )
            
            # Define project sources
            set(PROJECT_SOURCES
                ${CMAKE_CURRENT_LIST_DIR}/main.cpp
                ${CMAKE_CURRENT_LIST_DIR}/mainwindow.cpp
            
                # Widgets & views
                ${CMAKE_CURRENT_LIST_DIR}/widgets/tx_msg_config_entry_widget.cpp
                ${CMAKE_CURRENT_LIST_DIR}/widgets/can_clock_config_entry_widget.cpp
                ${CMAKE_CURRENT_LIST_DIR}/widgets/can_clock_config_entry_widget_timepar.cpp
                ${CMAKE_CURRENT_LIST_DIR}/widgets/statusbar.cpp
                ${CMAKE_CURRENT_LIST_DIR}/views/view_can_config.cpp
                ${CMAKE_CURRENT_LIST_DIR}/views/view_debug.cpp
                ${CMAKE_CURRENT_LIST_DIR}/views/view_logger.cpp
                ${CMAKE_CURRENT_LIST_DIR}/views/view_fw_update.cpp
                ${CMAKE_CURRENT_LIST_DIR}/views/view_tx_msg_config.cpp
                
                # Models
                ${CMAKE_CURRENT_LIST_DIR}/models/canloggerdatamodel.cpp
            
                # UI files
                ${CMAKE_CURRENT_LIST_DIR}/mainwindow.ui
                ${CMAKE_CURRENT_LIST_DIR}/widgets/can_clock_config_entry_widget.ui
                ${CMAKE_CURRENT_LIST_DIR}/widgets/tx_msg_config_entry_widget.ui
                ${CMAKE_CURRENT_LIST_DIR}/views/view_can_config.ui
                ${CMAKE_CURRENT_LIST_DIR}/views/view_logger.ui
                ${CMAKE_CURRENT_LIST_DIR}/views/view_fw_update.ui
                ${CMAKE_CURRENT_LIST_DIR}/views/view_debug.ui
                ${CMAKE_CURRENT_LIST_DIR}/views/view_tx_msg_config.ui
            
                # Missing this, MOC won't run for custom header files !!
                ${CMAKE_CURRENT_LIST_DIR}/views/custom_tab_widget_base.h
            )
            
            # Add mbedTLS
            file(GLOB MBEDTLS_FILES ${CMAKE_CURRENT_LIST_DIR}/../libs/mbedtls/library/*.c)
            set(PROJECT_SOURCES ${PROJECT_SOURCES} ${MBEDTLS_FILES})
            
            # Create executable file
            qt_add_executable(${CMAKE_PROJECT_NAME}
                MANUAL_FINALIZATION
                ${PROJECT_SOURCES}
            )
            
            # Set application firmware version -> read from the file directly
            include(../cmake/version.cmake)
            
            # Add to build
            target_compile_definitions(${CMAKE_PROJECT_NAME} PUBLIC
                # Version setup
                "CA_FW_VERSION_MAJOR=${CA_FW_VERSION_MAJOR}"
                "CA_FW_VERSION_MINOR=${CA_FW_VERSION_MINOR}"
                "CA_FW_VERSION_PATCH=${CA_FW_VERSION_PATCH}"
                "CA_FW_VERSION_TYPE=${CA_FW_VERSION_TYPE}"
                "CA_FW_VERSION_REV=${CA_FW_VERSION_REV}"
            
                # Nano protobuf
                "PB_FIELD_32BIT"
            )
            
            # Set include paths
            target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${PROJECT_INCLUDES})
            
            # Link Qt libraries
            target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
                Qt${QT_VERSION_MAJOR}::Widgets
                Qt${QT_VERSION_MAJOR}::Core
                Qt${QT_VERSION_MAJOR}::SerialPort
            )
            
            # Add cananalyzer library
            target_link_directories(${PROJECT_NAME} PUBLIC build/Win64-Debug)
            target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE libCANanalyzer)
            
            set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
                MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
                MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
                MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
                MACOSX_BUNDLE TRUE
                WIN32_EXECUTABLE OFF
            )
            
            install(TARGETS ${CMAKE_PROJECT_NAME}
                BUNDLE DESTINATION .
                LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
            
            if(QT_VERSION_MAJOR EQUAL 6)
                qt_finalize_executable(${CMAKE_PROJECT_NAME})
            endif()
            

            Start command

            gui_cpp\build\Win64-Debug>.\CANanalyzer.exe
            Before MainWindow constructor
            

            For instance, I got Before MainWindow constructor but then no After MainWindow message. No log messages are returned.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Then start it in your debugger and see why it crashes. Minimize the ctor until it works reliable.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • T tilz0R

              @Axel-Spoerl

              Compiled with CMake:

              # configure
              cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_TOOLCHAIN_FILE=D:/.../cmakefile/x86_64-w64-mingw32-gcc.cmake -SD:/.../gui_cpp -BD:/.../build/Win64-Debug -G Ninja
              # Build
              cmake --build path/to/dir
              

              Toolchain file

              set(CMAKE_SYSTEM_NAME               Windows)
              
              # Some default GCC settings
              set(CMAKE_C_COMPILER                x86_64-w64-mingw32-gcc)
              set(CMAKE_CXX_COMPILER              x86_64-w64-mingw32-g++)
              
              set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
              

              Toolchain

              x86_64-w64-mingw32-gcc --version
              x86_64-w64-mingw32-gcc (Rev6, Built by MSYS2 project) 12.2.0
              Copyright (C) 2022 Free Software Foundation, Inc.
              This is free software; see the source for copying conditions.  There is NO
              warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
              

              CMakeLists file

              cmake_minimum_required(VERSION 3.22)
              
              project("CANanalyzer" VERSION 0.1 LANGUAGES C CXX)
              
              set(CMAKE_AUTOMOC ON)
              set(CMAKE_AUTOUIC ON)
              set(CMAKE_AUTORCC ON)
              
              set(CMAKE_CXX_STANDARD          17)
              set(CMAKE_CXX_STANDARD_REQUIRED ON)
              #set(CMAKE_PREFIX_PATH "c:/Qt6.5.0/6.5.0/mingw_64/")
              
              find_package(QT NAMES Qt6 REQUIRED COMPONENTS Widgets)
              find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core)
              find_package(QT NAMES Qt6 REQUIRED COMPONENTS SerialPort)
              find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
              find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
              find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS SerialPort)
              
              # Define include paths
              set(PROJECT_INCLUDES
                  ${CMAKE_CURRENT_LIST_DIR}/
                  ${CMAKE_CURRENT_LIST_DIR}/include
                  ${CMAKE_CURRENT_LIST_DIR}/models
                  ${CMAKE_CURRENT_LIST_DIR}/widgetss
                  ${CMAKE_CURRENT_LIST_DIR}/views
                  ${CMAKE_CURRENT_LIST_DIR}/../common/include
                  ${CMAKE_CURRENT_LIST_DIR}/../common/proto
                  ${CMAKE_CURRENT_LIST_DIR}/../libs/nanopb
                  ${CMAKE_CURRENT_LIST_DIR}/../libs/mbedtls/include
              )
              
              # Define project sources
              set(PROJECT_SOURCES
                  ${CMAKE_CURRENT_LIST_DIR}/main.cpp
                  ${CMAKE_CURRENT_LIST_DIR}/mainwindow.cpp
              
                  # Widgets & views
                  ${CMAKE_CURRENT_LIST_DIR}/widgets/tx_msg_config_entry_widget.cpp
                  ${CMAKE_CURRENT_LIST_DIR}/widgets/can_clock_config_entry_widget.cpp
                  ${CMAKE_CURRENT_LIST_DIR}/widgets/can_clock_config_entry_widget_timepar.cpp
                  ${CMAKE_CURRENT_LIST_DIR}/widgets/statusbar.cpp
                  ${CMAKE_CURRENT_LIST_DIR}/views/view_can_config.cpp
                  ${CMAKE_CURRENT_LIST_DIR}/views/view_debug.cpp
                  ${CMAKE_CURRENT_LIST_DIR}/views/view_logger.cpp
                  ${CMAKE_CURRENT_LIST_DIR}/views/view_fw_update.cpp
                  ${CMAKE_CURRENT_LIST_DIR}/views/view_tx_msg_config.cpp
                  
                  # Models
                  ${CMAKE_CURRENT_LIST_DIR}/models/canloggerdatamodel.cpp
              
                  # UI files
                  ${CMAKE_CURRENT_LIST_DIR}/mainwindow.ui
                  ${CMAKE_CURRENT_LIST_DIR}/widgets/can_clock_config_entry_widget.ui
                  ${CMAKE_CURRENT_LIST_DIR}/widgets/tx_msg_config_entry_widget.ui
                  ${CMAKE_CURRENT_LIST_DIR}/views/view_can_config.ui
                  ${CMAKE_CURRENT_LIST_DIR}/views/view_logger.ui
                  ${CMAKE_CURRENT_LIST_DIR}/views/view_fw_update.ui
                  ${CMAKE_CURRENT_LIST_DIR}/views/view_debug.ui
                  ${CMAKE_CURRENT_LIST_DIR}/views/view_tx_msg_config.ui
              
                  # Missing this, MOC won't run for custom header files !!
                  ${CMAKE_CURRENT_LIST_DIR}/views/custom_tab_widget_base.h
              )
              
              # Add mbedTLS
              file(GLOB MBEDTLS_FILES ${CMAKE_CURRENT_LIST_DIR}/../libs/mbedtls/library/*.c)
              set(PROJECT_SOURCES ${PROJECT_SOURCES} ${MBEDTLS_FILES})
              
              # Create executable file
              qt_add_executable(${CMAKE_PROJECT_NAME}
                  MANUAL_FINALIZATION
                  ${PROJECT_SOURCES}
              )
              
              # Set application firmware version -> read from the file directly
              include(../cmake/version.cmake)
              
              # Add to build
              target_compile_definitions(${CMAKE_PROJECT_NAME} PUBLIC
                  # Version setup
                  "CA_FW_VERSION_MAJOR=${CA_FW_VERSION_MAJOR}"
                  "CA_FW_VERSION_MINOR=${CA_FW_VERSION_MINOR}"
                  "CA_FW_VERSION_PATCH=${CA_FW_VERSION_PATCH}"
                  "CA_FW_VERSION_TYPE=${CA_FW_VERSION_TYPE}"
                  "CA_FW_VERSION_REV=${CA_FW_VERSION_REV}"
              
                  # Nano protobuf
                  "PB_FIELD_32BIT"
              )
              
              # Set include paths
              target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${PROJECT_INCLUDES})
              
              # Link Qt libraries
              target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
                  Qt${QT_VERSION_MAJOR}::Widgets
                  Qt${QT_VERSION_MAJOR}::Core
                  Qt${QT_VERSION_MAJOR}::SerialPort
              )
              
              # Add cananalyzer library
              target_link_directories(${PROJECT_NAME} PUBLIC build/Win64-Debug)
              target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE libCANanalyzer)
              
              set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
                  MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
                  MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
                  MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
                  MACOSX_BUNDLE TRUE
                  WIN32_EXECUTABLE OFF
              )
              
              install(TARGETS ${CMAKE_PROJECT_NAME}
                  BUNDLE DESTINATION .
                  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
              
              if(QT_VERSION_MAJOR EQUAL 6)
                  qt_finalize_executable(${CMAKE_PROJECT_NAME})
              endif()
              

              Start command

              gui_cpp\build\Win64-Debug>.\CANanalyzer.exe
              Before MainWindow constructor
              

              For instance, I got Before MainWindow constructor but then no After MainWindow message. No log messages are returned.

              Axel SpoerlA Offline
              Axel SpoerlA Offline
              Axel Spoerl
              Moderators
              wrote on last edited by
              #5

              @tilz0R
              Ok, but the before constructor message is always printed, right?

              Software Engineer
              The Qt Company, Oslo

              1 Reply Last reply
              0
              • T tilz0R

                I'm learning Qt for C++ (with Python I made pretty good progress) and I have notices that sometimes my window won't pop-up, logs in the window constructor won't get printed (qDebug()<<"message..." and similar type of messages).

                I'm communicating with my DLL to access serial port, so I though this may have an impact, but after removing it, I don't see difference. Still, out of 10 attempts to start .exe, Window will show maybe around 60% of the times.

                Some other info:

                • Windows 11
                • Qt version 6.5
                • CMake as build system, developed under vscode
                • Qt paths set accordingly
                • There is no error at all, application seems to start and do something in the background, but windows doesn't open

                If it's needed, below is main.c and mainwindow.cpp.

                #include "mainwindow.h"
                #include <QApplication>
                
                int
                main(int argc, char* argv[]) {
                    QApplication a(argc, argv);
                    
                    //These 2 prints sometimes don't show in the console at all
                    qDebug() << "Before MainWindow constructor";
                    MainWindow w;
                    qDebug() << "After MainWindow constructor";
                    w.show();
                    return a.exec();
                }
                

                mainwindow class constructor

                MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
                    ui->setupUi(this);
                
                    /* Create custom views */
                    view_can_config = new ViewCanConfig();
                    view_debug = new ViewDebug();
                    view_fw_update = new ViewFwUpdate();
                    view_logger = new ViewLogger();
                    view_tx_msg_config = new ViewTxMsgConfig();
                    views_list.append(view_can_config);
                    views_list.append(view_debug);
                    views_list.append(view_fw_update);
                    views_list.append(view_logger);
                    views_list.append(view_tx_msg_config);
                    connect(view_tx_msg_config, ViewTxMsgConfig::onSendMessage, this, MainWindow::send_can_message);
                    connect(view_logger, ViewLogger::onSendMessage, this, MainWindow::send_can_message);
                
                    /* Connect debug messages */
                    connect(view_can_config, ViewCanConfig::send_debug, view_debug, ViewDebug::add_debug_message);
                    connect(view_debug, ViewDebug::send_debug, view_debug, ViewDebug::add_debug_message);
                    connect(view_fw_update, ViewFwUpdate::send_debug, view_debug, ViewDebug::add_debug_message);
                    connect(view_logger, ViewLogger::send_debug, view_debug, ViewDebug::add_debug_message);
                    connect(view_tx_msg_config, ViewTxMsgConfig::send_debug, view_debug, ViewDebug::add_debug_message);
                    connect(this, MainWindow::send_debug, view_debug, ViewDebug::add_debug_message);
                
                    /* Add views to the layouts */
                    ui->layout_tab_logger->addWidget(view_logger);
                    ui->layout_tab_debug->addWidget(view_debug);
                    ui->layout_tab_fdcan_config->addWidget(view_can_config);
                    ui->layout_tab_tx_msg_config->addWidget(view_tx_msg_config);
                    ui->layout_tab_fw_update->addWidget(view_fw_update);
                
                    /* Add quick TX widget to the logger view using default configuration */
                    view_logger->add_quick_tx_widget(view_tx_msg_config->create_new_tx_msg_config_window());
                
                    /* Set status bar */
                    statusbar = new StatusBar(this);
                    ui->statusbar->addWidget(statusbar);
                
                    /* Triggers */
                    connect(ui->btn_com_port_refresh, &QPushButton::clicked, this, &MainWindow::load_com_ports);
                    connect(ui->btn_com_port_connect_disconnect, &QPushButton::clicked, this, &MainWindow::toggle_connect_to_port);
                
                    /* Get system com ports */
                #if 1
                    load_com_ports();
                
                    /* Reset values */
                    ca_evt_read_ptr = 0;
                    ca_evt_write_ptr = 0;
                
                    /* Queue processing timer */
                    timer_queue = new QTimer(this);
                    timer_queue->setSingleShot(false);
                    timer_queue->setInterval(50);
                    connect(timer_queue, &QTimer::timeout, this, &MainWindow::timer_ca_queue_check_expired);
                    timer_queue->start();
                
                    emit send_debug(CaUtils::DebugLevel::Info, "APP", "Application running");
                
                    /* Initialize CANanalyzer interface */
                    if (ca_init((ca_evt_fn)&prv_ca_evt_fn)) {
                        emit send_debug(CaUtils::DebugLevel::Info, "DLL", "App ready to use");
                    }
                #endif
                }
                

                Am I missing something? What could be common reasons that app wouldn't always start properly?

                Axel SpoerlA Offline
                Axel SpoerlA Offline
                Axel Spoerl
                Moderators
                wrote on last edited by
                #6

                @tilz0R said in Application won't always start:

                load_com_ports();

                As @Christian-Ehrlicher said, you could start by commenting this line out.

                I have no clue what the application actually does, but com ports can be a bit flaky at times. So maybe this function blocks.

                Software Engineer
                The Qt Company, Oslo

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tilz0R
                  wrote on last edited by
                  #7

                  Found the problem. It was uninitialized variable in another class that was constructed in the constructor of MainWindow, causing for loop to go to to 2^32 or other random number.

                  Sorry for taking your time for this.

                  The Before MainWindow... was NOT always printed. It may have to do with flushing of the data in the system.

                  I will continue monitoring but it looks good for the moment.

                  1 Reply Last reply
                  0
                  • T tilz0R has marked this topic as solved on

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved