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. How to write this QT program as class
QtWS25 Last Chance

How to write this QT program as class

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 169 Views
  • 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.
  • E Offline
    E Offline
    ElfMoon
    wrote on last edited by
    #1

    I'm new in QT, this is code I copy from another source (which I forget).
    I dont see class declaration anywhere in this code
    How to re-write it using class?

    /* QGridLayout */
    #include <QApplication>
    #include <QGroupBox>
    #include <QLabel>
    #include <QLineEdit>
    #include <QMainWindow>
    #include <QPushButton>
    #include <QTextEdit>
    #include <QVBoxLayout>
    #include <QWidget>
    
    #include "mainwindow.h"
    
    int main( int argc, char *argv[] ) {
    	QApplication a( argc, argv );
    
    	QMainWindow window;
    	QWidget *widget = new QWidget( &window );
    	QGridLayout *layout = new QGridLayout( widget );
    	window.setCentralWidget( widget );
    	widget->setLayout( layout );
    	QGroupBox *box = new QGroupBox( "Information:", widget );
    	layout->addWidget( box, 0, 0 );
    
    	QVBoxLayout *boxLayout = new QVBoxLayout( box );
    	QWidget *nameWidget = new QWidget( box );
    	QWidget *ageWidget = new QWidget( box );
    	QWidget *addressWidget = new QWidget( box );
    
    	boxLayout->addWidget( nameWidget );
    	boxLayout->addWidget( ageWidget );
    	boxLayout->addWidget( addressWidget );
    
    	QHBoxLayout *nameLayout = new QHBoxLayout( nameWidget );
    	nameLayout->addWidget( new QLabel( "Name:" ) );
    	nameLayout->addWidget( new QLineEdit( nameWidget ) );
    
    	QHBoxLayout *ageLayout = new QHBoxLayout( ageWidget );
    	ageLayout->addWidget( new QLabel( "Age:" ) );
    	ageLayout->addWidget( new QLineEdit( ageWidget ) );
    
    	QHBoxLayout *addressLayout = new QHBoxLayout( addressWidget );
    	addressLayout->addWidget( new QLabel( "Address:" ) );
    	addressLayout->addWidget( new QLineEdit( addressWidget ) );
    
    	layout->addWidget( new QPushButton( "Validate", widget ), 1, 0 );
    	layout->addWidget( new QPushButton( "Reset", widget ), 1, 1 );
    	layout->addWidget( new QPushButton( "Cancel", widget ), 1, 2 );
    	window.show();
    
    	return a.exec();
    }
    
    
    
    jsulmJ 1 Reply Last reply
    0
    • E ElfMoon

      I'm new in QT, this is code I copy from another source (which I forget).
      I dont see class declaration anywhere in this code
      How to re-write it using class?

      /* QGridLayout */
      #include <QApplication>
      #include <QGroupBox>
      #include <QLabel>
      #include <QLineEdit>
      #include <QMainWindow>
      #include <QPushButton>
      #include <QTextEdit>
      #include <QVBoxLayout>
      #include <QWidget>
      
      #include "mainwindow.h"
      
      int main( int argc, char *argv[] ) {
      	QApplication a( argc, argv );
      
      	QMainWindow window;
      	QWidget *widget = new QWidget( &window );
      	QGridLayout *layout = new QGridLayout( widget );
      	window.setCentralWidget( widget );
      	widget->setLayout( layout );
      	QGroupBox *box = new QGroupBox( "Information:", widget );
      	layout->addWidget( box, 0, 0 );
      
      	QVBoxLayout *boxLayout = new QVBoxLayout( box );
      	QWidget *nameWidget = new QWidget( box );
      	QWidget *ageWidget = new QWidget( box );
      	QWidget *addressWidget = new QWidget( box );
      
      	boxLayout->addWidget( nameWidget );
      	boxLayout->addWidget( ageWidget );
      	boxLayout->addWidget( addressWidget );
      
      	QHBoxLayout *nameLayout = new QHBoxLayout( nameWidget );
      	nameLayout->addWidget( new QLabel( "Name:" ) );
      	nameLayout->addWidget( new QLineEdit( nameWidget ) );
      
      	QHBoxLayout *ageLayout = new QHBoxLayout( ageWidget );
      	ageLayout->addWidget( new QLabel( "Age:" ) );
      	ageLayout->addWidget( new QLineEdit( ageWidget ) );
      
      	QHBoxLayout *addressLayout = new QHBoxLayout( addressWidget );
      	addressLayout->addWidget( new QLabel( "Address:" ) );
      	addressLayout->addWidget( new QLineEdit( addressWidget ) );
      
      	layout->addWidget( new QPushButton( "Validate", widget ), 1, 0 );
      	layout->addWidget( new QPushButton( "Reset", widget ), 1, 1 );
      	layout->addWidget( new QPushButton( "Cancel", widget ), 1, 2 );
      	window.show();
      
      	return a.exec();
      }
      
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ElfMoon Quite generic question.
      Why do you want to invent a class for this small piece of code?

      If you want to add a class here then one way to do that in this particular case is to subclass QMainWindow class and move all the UI related stuff into that new class. If you don't know how to do that then I suggest to create a QtWidgets default project in QtCreator and take a look at it.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3

      • Login

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