Difference between revisions of "Qt5/Qt-Creator Debugging Window in a Splitter"
(→Video) |
|||
Line 11: | Line 11: | ||
<videoflash>haqpipMxq_c</videoflash> | <videoflash>haqpipMxq_c</videoflash> | ||
</center> | </center> | ||
+ | <br /> | ||
+ | =Formatted Output with QStrings= | ||
+ | <br /> | ||
+ | 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: | ||
+ | <br /> | ||
+ | <source lang="cpp"> | ||
+ | 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 ) ); | ||
+ | |||
+ | </source> | ||
+ | |||
<br /> | <br /> | ||
Revision as of 12:22, 6 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.
Contents
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 ) );
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>