Qt/Widgets/Time

From ProgrammingExamples
< Qt
Revision as of 13:03, 14 February 2011 by Daviddoria (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

QTime.cpp

#include <QProgressDialog>
#include <QApplication>
 
#include "form.h"
 
int main( int argc, char **argv )
{
  QApplication app(argc, argv);
  MainWindow window;
 
  window.show();
 
  return app.exec();
}

form.h

#ifndef FORM_H
#define FORM_H
 
#include "ui_main.h"
 
#include <QTime>
 
class MainWindow : public QMainWindow, public Ui::MainWindow
{
Q_OBJECT
 
public:
  MainWindow();
 
public slots:
  void btnDisplayTime_clicked();
 
private:
    QTime timer;
};
 
#endif

form.cpp

#include <QtGui>
 
#include "form.h"
#include <iostream>
 
MainWindow::MainWindow()
{
  setupUi(this);
  connect(this->btnDisplayTime, SIGNAL(clicked()), this, SLOT(btnDisplayTime_clicked()));
 
}
 
void MainWindow::btnDisplayTime_clicked()
{
  this->timer.start();
  QString format = "hh:mm:ss";
  std::cout << this->timer.toString(format).toStdString() << std::endl;
  this->lblTime->setText(this->timer.toString(format).toStdString().c_str());
  this->lblTime->setText(this->timer.toString(format));
}

main.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>205</width>
    <height>169</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="btnDisplayTime">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>30</y>
      <width>91</width>
      <height>28</height>
     </rect>
    </property>
    <property name="text">
     <string>Display time</string>
    </property>
   </widget>
   <widget class="QLabel" name="lblTime">
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>120</y>
      <width>59</width>
      <height>17</height>
     </rect>
    </property>
    <property name="text">
     <string>Time</string>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(QTime)
 
FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE(${QT_USE_FILE})
 
QT4_WRAP_UI(UISrcs main.ui)
QT4_WRAP_CPP(MOCSrcs form.h)
 
include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
 
ADD_EXECUTABLE(QTime QTime.cpp form.cpp ${UISrcs} ${MOCSrcs})
TARGET_LINK_LIBRARIES(QTime ${QT_LIBRARIES})