帮助开始使用Qt设计基本的GUI

时间:2011-04-18 14:25:49

标签: qt user-interface qt4

对于我们的学期实验室考试,我们需要制作一个GUI,列出不同类别下的所有程序,并在我们点击其中一个时打开它们。 我不太了解GUI编程,但了解C ++的基础知识,并认为Qt将是一个很好的选择。 我正在使用QtCreator,它似乎是一个非常漂亮的工具。

我想要实现的是,当我点击一个按钮时,应该隐藏主窗口,并且应该从我将执行所需程序的位置显示相应类别的窗口。我正在使用两个不同的类,它们分别从主窗口和进程管理类别的HomeWidget和ProcessWidget继承QWidget。我计划为其他类别添加更多小部件,但我不清楚如何将这些小部件粘合在一起。

我认为我所做的设计选择并不合适,而且我不确定合适的设计选择。关于这方面的帮助将非常感激,我正在使用的所有文件的内容都附在下面。

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

如果我不清楚解释我的问题,我会非常乐意澄清。

2 个答案:

答案 0 :(得分:1)

查看QStackedWidget(http://doc.qt.nokia.com/latest/qstackedwidget.html)。如果您将QStackedWidget作为窗口的主窗口小部件,然后将HomeWidget和ProcessWidget放在QStackedWidget中,它将允许您切换显示的窗口小部件。

答案 1 :(得分:0)

我认为你根本不需要ProcessWidget。您可能需要MainWidget(主要用于指定实验室属性,如名称,任务描述等)。

您可以使用QFileDialog搜索可执行文件(或您想要的任何类型的文件)。

要运行此文件,您可以使用QProcess

给定课程的Qt参考可以为您提供比其他任何人更多的额外信息。