这是一个模拟Qt框架的程序,要求完善各个类的成员函数的定义,并实现测试的输出,具体的要求请看程序中的注释。
函数接口定义:
在这里描述函数接口:
//控件的顶级类
class DQWidget{
protected:
DQWidget* parent;//父控件指针
public:
void setParent(DQWidget* p = NULL);
virtual void show() {}
};class DQPushButton:public DQWidget{
protected:
string text;
public:
void setText(string text);
void show();
};//抽象控件
class DQAbstractWidget:public DQWidget{
protected:
int value; //控件的当前值
int min; //控件的最小值
int max; //控件的最大值
public:
void setValue(int value);
void setRange(int min, int max);;
void show(); //显示控件的当前值和范围(最小值和最大值)
};//旋转框
class DQSpinBox : public DQAbstractWidget{
public:
//显示本控件的类型,并调用父类的show显示值和范围
void show();
};//滑杆控件类
class DQSlider : public DQAbstractWidget{
public:
//显示本控件的类型,并调用父类的show显示值和范围
void show();
};//横向布局类
class DQHBoxLayout{
//布局中共包含N个控件的指针
DQWidget **cwidgets;
public:
//构造函数中应对控件数组初始化
DQHBoxLayout();
//添加一个控件到布局中
void addWidget(DQWidget *w);
//读取布局中的控件数组
DQWidget **getWidgets();
};//主窗口类
class DQMainWindow:public DQWidget{
int width,height; //主窗口的宽度和高度
string windowTitle; //窗口标题
DQHBoxLayout *layout; //窗口的布局
public:
DQMainWindow(DQWidget* parent = NULL) {}
void setWindowTitle(string windowTitle);
//设置窗口的布局
void setLayout(DQHBoxLayout *lout);
//改变窗口的大小
void resize(int width,int height);
//显示窗口,包括
//1. 窗口部件的自身的信息:类型、标题、大小
//2. 显示窗口中的所有控件
void show();
};//Qt应用程序类
class DQApplication{
public:
DQApplication(int argc, char **argv) {}
int exec(){return 0;}
};
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
int main(int argc,char** argv)
{
int mode,min,max,value,width,height;
cin>>mode>>min>>max>>value>>width>>height;
//1.创建Qt应用程序对象
DQApplication app(argc, argv);
//2.创建主窗口
DQMainWindow *window = new DQMainWindow;
window->setWindowTitle("First Qt Application!");
window->resize(width,height);
//3.创建Layout,并用来将控件进行布局,这是一个横向布局
DQHBoxLayout *layout = new DQHBoxLayout;
DQAbstractWidget *spinBox,*slider;
DQPushButton *okBtn,*cancelBtn;
//4.创建窗口中的控件,并设置相应的属性
switch(mode)
{
case 1:
spinBox = new DQSpinBox;
spinBox->setRange(min,max);
spinBox->setValue(value);
spinBox->show();
break;
case 2:
slider = new DQSlider;
slider->setRange(min,max);
slider->setValue(value);
slider->show();
break;
case 3:
okBtn = new DQPushButton;
okBtn->setText("OK");
okBtn->show();
break;
default:
spinBox = new DQSpinBox;
spinBox->setRange(min,max);
spinBox->setValue(value);
slider = new DQSlider;
slider->setRange(min,max);
slider->setValue(value);
okBtn = new DQPushButton;
okBtn->setText("OK");
cancelBtn = new DQPushButton;
cancelBtn->setText("Cancel");
//5.添加控件到布局里
layout->addWidget(spinBox);
layout->addWidget(slider);
layout->addWidget(okBtn);
layout->addWidget(cancelBtn);
//6.设置主窗口的布局
window->setLayout(layout);
//7.显示主窗口
window->show();
}
return app.exec();
}
/* 请在这里填写答案 */
输入样例1:
在这里给出一组输入:
1 0 100 30 1024 768
输出样例1:
在这里给出相应的输出。例如:
This is a SpinBox
Value = 30
Range is 0 to 100
输入样例2:
在这里给出一组输入:
2 0 100 30 1024 768
输出样例2:
在这里给出相应的输出。例如:
This is a Slider
Value = 30
Range is 0 to 100
输入样例3:
在这里给出一组输入:
4 0 100 30 1024 768
输出样例3:
在这里给出相应的输出。例如:
This is a DQWindow
Title is First Qt Application!
my window's size is(1024,768)
This is a SpinBox
Value = 30
Range is 0 to 100
This is a Slider
Value = 30
Range is 0 to 100
This is a DQPushButton
The text is OK
This is a DQPushButton
The text is Cancel
我的答案
void DQWidget::setParent(DQWidget* p) { parent = p; }
void DQAbstractWidget::setValue(int value) { this->value = value; }
void DQAbstractWidget::setRange(int min, int max)
{
this->min = min, this->max = max;
}
void DQAbstractWidget::show()
{
cout << "Value = " << value << endl;
cout << "Range is " << min << " to " << max << endl;
}
void DQSpinBox::show()
{
cout << "This is a SpinBox" << endl;
DQAbstractWidget::show();
}
void DQSlider::show()
{
cout<<"This is a Slider" << endl;
DQAbstractWidget::show();
}
void DQPushButton::setText(string text)
{
this->text = text;
}
void DQPushButton::show()
{
cout << "This is a DQPushButton" << endl;
cout << "The text is " << text << endl;
}
DQHBoxLayout::DQHBoxLayout()
{
cwidgets = new DQWidget * [N];
for (int i = 0; i < N; i++) {
cwidgets[i] = NULL;
}
}
void DQHBoxLayout::addWidget(DQWidget* w)
{
for (int i = 0; i < N; i++) {
if (!cwidgets[i]) {
cwidgets[i] = w;
return;
}
}
}
DQWidget** DQHBoxLayout::getWidgets() { return cwidgets; }
void DQMainWindow::setWindowTitle(string windowTitle)
{
this->windowTitle = windowTitle;
}
void DQMainWindow::setLayout(DQHBoxLayout* lout)
{
this->layout = lout;
}
void DQMainWindow::resize(int width, int height)
{
this->width = width;
this->height = height;
}
void DQMainWindow::show()
{
cout << "This is a DQWindow" << endl;
cout << "Title is " << windowTitle << endl;
cout << "my window's size is(" << width << "," << height << ")" << endl;
if (layout != NULL) {
DQWidget** cwidget = layout->getWidgets();
for (int i = 0; i < N; i++) {
if (cwidget[i] != NULL) {
cwidget[i]->show();
}
}
}
}