Difference between revisions of "Qt5/Qt-Creator Debugging Window in a Splitter"

From dftwiki3
Jump to: navigation, search
(Starting the App with the Debug Window Closed)
(Starting the App with the Debug Window Closed)
 
Line 30: Line 30:
 
=Starting the App with the Debug Window Closed=
 
=Starting the App with the Debug Window Closed=
 
<br />
 
<br />
The trick here is to set the size of the top widget to its natural size, and the size of the debug window to 0.  One way to do this is show below:
+
The trick here is to set the size of the top widget to its natural size, and the size of the debug window to 0.  One way to do this is show below; we add a few lines of code to the constructor so that we create the initial GUI with the debug window closed.
 
<br />
 
<br />
 
<source lang="cpp" highlight="6,7,8">
 
<source lang="cpp" highlight="6,7,8">

Latest revision as of 15:07, 8 February 2014

--D. Thiebaut (talk) 09:12, 6 February 2014 (EST)



This toturial presents a simple set of steps needed to add a debugging window at the bottom of the GUI of a Qt-5 application.







Video



Formatted Output with QStrings


Printing multiple arguments is easily accomplished using the arg() method of QString objects. The examples below illustrates different ways to print several pieces of information:

int n = 3;
float x = 10.5;

QString s = QString( "n = %1  x = %2\n" ).arg( n ).arg( x )
debug( s );

debug( QString( "%1: n=%3, x=%2" ).arg( "Initialization" ).arg( x ).arg( n ) );


Starting the App with the Debug Window Closed


The trick here is to set the size of the top widget to its natural size, and the size of the debug window to 0. One way to do this is show below; we add a few lines of code to the constructor so that we create the initial GUI with the debug window closed.

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow) {
  ui->setupUi(this);

  QList<int> heights;
  heights << ui->calendarWidget->height() << 0;
  ui->splitter->setSizes( heights );
}


Just remember to #include <QList> at the top of the C++ file!

Source Files


debugDemo.pro


#-------------------------------------------------
#
# Project created by QtCreator 2014-02-06T08:24:43
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = debugDemo
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui


main.cpp


 
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[]){
  QApplication a(argc, argv);
  MainWindow w;
  w.show();

  return a.exec();
}



mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow) {
  ui->setupUi(this);
}

MainWindow::~MainWindow() {
  delete ui;
}

void MainWindow::debug( QString s ) {
    ui->textBrowser->append( s );
}

void MainWindow::on_pushButton_clicked() {
    debug( "Click!" );
}



mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
  class MainWindow;
}

class MainWindow : public QMainWindow {
  Q_OBJECT

public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();
  void debug( QString s );

private slots:
  void on_pushButton_clicked();

private:
  Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H



mainwindow.ui


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>438</width>
    <height>454</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
       <widget class="QLabel" name="label">
        <property name="text">
         <string>File</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QLineEdit" name="lineEdit"/>
      </item>
      <item>
       <widget class="QPushButton" name="pushButton">
        <property name="text">
         <string>Browse...</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item row="1" column="0">
     <widget class="QSplitter" name="splitter">
      <property name="orientation">
       <enum>Qt::Vertical</enum>
      </property>
      <widget class="QCalendarWidget" name="calendarWidget"/>
      <widget class="QTextBrowser" name="textBrowser"/>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>438</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>