對於我們的學期實驗室考試,我們需要製作一個GUI,其中列出了不同類別下的所有程序,並在我們點擊其中一個時打開它們。 我不是很瞭解GUI編程,但知道C++的基礎知識,並認爲Qt將是一個很好的選擇。 我正在使用QtCreator,它看起來像一個漂亮的光滑的工具。幫助Qt開始設計基本的GUI
我想要實現的是,當我點擊一個按鈕,主窗口應該隱藏,相應類別的窗口應顯示從哪裏執行所需的程序。我使用了兩個不同的類,分別從主窗口和進程管理類別的QWidget繼承HomeWidget和ProcessWidget。我打算爲其他類別添加更多小部件,但我不完全清楚如何將這些單獨的小部件粘合在一起。
我認爲我所做的設計選擇是不恰當的,我不確定適當的設計選擇。有關這方面的幫助將不勝感激,我使用的所有文件的內容如下所示。
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 QLabel("<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();
}
最後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
我會更樂意澄清,如果我沒有解釋我的問題是明確的。
這看起來很有希望, 但我如何切換顯示的小部件。 – nikhil 2011-04-19 01:52:21
正如我在鏈接到的文檔中所說的(並且也提供了示例),您可以使用setCurrentIndex或setCurrentWidget函數。最有可能的是,您會將其中一個插槽連接到您的某個控件發出的信號。 – Colin 2011-04-19 20:54:18