For our semester lab examination, we need to make a GUI which lists all the programs under different categories and opens them when we'd click on one of them. I don't really know GUI programming but know the basics of C++ and thought that Qt would be a good choice to start with. I'm working with QtCreator and it seems like a pretty slick tool.
What i'm trying to achieve is that when i click a button, the main window should be hidden and the window for the corresponding category should be shown from where i'll execute the required program. I'm using two different classes that inherit from QWidget the HomeWidget and the ProcessWidget for the main window and the process management category respectively. I plan to add more widgets for the other categories but i'm not exactly clear as to how i can glue together these individual widgets.
I think that the design choice that i've made isn't appropriate, and i'm not sure of the appropriate design choice. Help regarding this would be much appreciated, the contents of all the files that i'm using is attached below.
homewidget.h
#ifndef HOMEWIDGET_H
#define HOMEWIDGET_H
#include <QtGui/QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include "processwidget.h"
class HomeWidget : public QWidget
{
Q_OBJECT
private:
QVBoxLayout *main_layout;
QHBoxLayout *footer;
QLabel *header,*footer_text;
QPushButton *process_mgt,*file_mgt;
QWidget *target;
public:
HomeWidget(QWidget *parent = 0);
~HomeWidget();
private slots:
void process_management_slot(QWidget *qw);
};
#endif // HOMEWIDGET_H
homewidget.cpp
#include "homewidget.h"
HomeWidget::HomeWidget(QWidget *parent)
: QWidget(parent)
{
main_layout = new QVBoxLayout();
footer = new QHBoxLayout();
header = new QLabel("<i>MCA Semester 2</i>\nOS LAB Record");
footer_text = new QLabel("Made By :\n\t Nikhil Bhardwaj");
process_mgt = new QPushButton("Process Management");
file_mgt = new QPushButton("File Management");
setWindowTitle("Operating Systems Lab, MCA NITT 2011");
footer->addStretch();
footer->addWidget(footer_text);
main_layout->addWidget(header);
main_layout->addStretch();
main_layout->addWidget(process_mgt);
main_layout->addWidget(file_mgt);
main_layout->addStretch();
main_layout->addLayout(footer);
setLayout(main_layout);
connect(process_mgt,SIGNAL(clicked()),this,SLOT(process_management_slot(process_widget)));
}
HomeWidget::~HomeWidget()
{
}
void HomeWidget::process_management_slot(QWidget *qw)
{
this->hide();
target = qw;
target->show();
}
processwidget.h
#ifndef PROCESSWIDGET_H
#define PROCESSWIDGET_H
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
class ProcessWidget : public QWidget
{
Q_OBJECT
private:
QVBoxLayout *process_layout;
QHBoxLayout *process_footer;
QLabel *process_header,*process_footer_text;
QPushButton *back,*scheduling,*synchronization,*bankers;
public:
ProcessWidget(QWidget *parent = 0);
signals:
public slots:
};
#endif // PROCESSWIDGET_H
processwidget.cpp
#include "processwidget.h"
ProcessWidget::ProcessWidget(QWidget *parent) :
QWidget(parent)
{
process_layout = new QVBoxLayout();
process_footer = new QHBoxLayout();
process_header = new Q开发者_JAVA百科Label("<i>MCA Semester 2</i>\nOS LAB Record");
process_footer_text = new QLabel("Made By :\n\t Nikhil Bhardwaj");
back = new QPushButton("Main Menu");
scheduling = new QPushButton("Scheduling Algorithms");
setWindowTitle("Operating Systems Lab, Process Management");
process_footer->addStretch();
process_footer->addWidget(process_footer_text);
process_layout->addWidget(process_header);
process_layout->addStretch();
process_layout->addWidget(scheduling);
process_layout->addWidget(back);
process_layout->addStretch();
process_layout->addLayout(process_footer);
setLayout(process_layout);
}
main.cpp
#include <QtGui/QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include "homewidget.h"
#include "processwidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
HomeWidget *main_widget = new HomeWidget();
ProcessWidget *process_widget = new ProcessWidget();
main_widget->show();
return a.exec();
}
and finally final_gui.pro
#-------------------------------------------------
#
# Project created by QtCreator 2011-04-15T06:37:21
#
#-------------------------------------------------
QT += core gui
TARGET = final_gui
TEMPLATE = app
SOURCES += main.cpp\
homewidget.cpp \
processwidget.cpp
HEADERS += homewidget.h \
processwidget.h
I'll be more than happy to clarify if i haven't been clear in explaining my issue.
Take a look at QStackedWidget (http://doc.qt.nokia.com/latest/qstackedwidget.html). If you make the QStackedWidget the main widget for your window, and then put the HomeWidget and ProcessWidget in the QStackedWidget, it will allow you to switch which widget is displayed.
I think you don't really need ProcessWidget at all. You maybe need MainWidget (mainly to specify lab properties like name, task description, etc).
You can use QFileDialog to search for executable files (or whatever type of files you want).
For running this files you can use QProcess.
Qt reference for given classes can give you more additional information then anyone else.
精彩评论