qt 之 mainwidget

来源:互联网 发布:范德堡大学知乎 编辑:程序博客网 时间:2024/06/11 18:43

1、新建菜单

mainwindow类里添加的

private:

    Ui::MainWindow *ui;
    QMenu *menuBox[5];    QAction *actionFile[6];

mainwindow构造函数里的

        menuBox[0]=menuBar()->addMenu(tr("&File"));     //新建菜单项

    actionFile[0] = new QAction(tr("Load MIB"), this);  //向菜单项里添加选项
    actionFile[1] = new QAction(tr("Upload MIB"), this);
    actionFile[2] = new QAction(tr("MIB Module"), this);
    actionFile[3] = new QAction(tr("Open session"), this);
    actionFile[4] = new QAction(tr("Save session"), this);
    actionFile[5] = new QAction(tr("Exit"), this);2、工具栏maniwindow类里添加的    QToolBar *toolbar;mainwindow构造函数里的    toolbar = addToolBar(tr("Button"));
    QAction *start = new QAction(tr("&Start"), this);
    start->setIcon(QIcon(tr(":/pushButton/x.JPG")));  //设置图标,此图标资源文件里要有    start->setToolTip(tr("当鼠标指向时候提示"));      //当鼠标指向时提示    start->setStatusTip(tr("当鼠标指向时,状态栏提示"));//当鼠标指向时,状态栏提示
    toolbar->setSeparator(1);                           //添加分隔线
toolbar->setStyleSheet("QToolBar{border:0}"); //将突起的边框隐藏掉
    QAction *stop = new QAction(tr("&Stop"), this);
    toolbar->addAction(start);
    toolbar->addAction(stop);    toolbar->setOrientation(Qt::Vertical);    //设置排列方式要修改toolbar的位置可以定义一个layout然后将toolbar添加进去    layout->addwidget(toolbar);3、设置中央部分mainwidget类里添加   QWidget *widget;   QVBoxLayout *vBoxLayout1;
   QVBoxLayout *vBoxLayout2;
   QVBoxLayout *vBoxLayout3;
QHBoxLayout *hBoxLayoutCenture;
mainwidget构造函数里
vBoxLayout1 = new QVBoxLayout;
vBoxLayout2 = new QVBoxLayout;
vBoxLayout3 = new QVBoxLayout;
hBoxLayoutCenture = new QHBoxLayout;
vBoxLayoutTotal = new QVBoxLayout();

hBoxLayoutCenture->addLayout(vBoxLayout1); //布局主窗口
   hBoxLayoutCenture->addLayout(vBoxLayout2);
   hBoxLayoutCenture->addLayout(vBoxLayout3);

vBoxLayoutTotal->addLayout(hBoxLayoutCenture);

widget = new QWidget();
   widget->setLayout(vBoxLayoutTotal); //对主窗口进行封装

this->setCentralWidget(widget); //设置主窗口

4、关于Button鼠标捕获
需要重载mouseEvent()函数
在mainwindow类里添加
protected:
    void mouseMoveEvent(QMouseEvent *e);
在mainwindow构造函数里设置
this->setMouseTracking(true);
widget->setMouseTracking(true);
pushButtonGO->setMouseTracking(1); //必须这样层层设置鼠标捕获使能才可以
下面贴一下mouseMoveEvetn()函数原型
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
    if(((e->x() >= pushButtonGO->x()) &&
       (e->x() <= (pushButtonGO->x() + 90))) &&
       ((e->y() >= (pushButtonGO->y() + 40) &&
       (e->y()) <= (pushButtonGO->y() + 60))))
    {
        pushButtonGO->setFlat(0);
    }
    else
    {
        pushButtonGO->setFlat(1);
    }
    qDebug() << tr("Mouse x posion is:") << e->x();
    qDebug() << tr("Mouse y posion is:") << e->y();
    qDebug() << tr("pushbutton x posion is:") << pushButtonGO->x();
    qDebug() << tr("pushbutton y posion is:") << pushButtonGO->y();
}

5、qtreewidget使用
在mainwidget的类里定义
QTreeWidget *treeWidgetSNMP;
mainwidget构造函数里
treeWidgetSNMP = new QTreeWidget;
    QStringList headers;    //表头列表
    headers << tr("SNMP2") << tr("2");
    treeWidgetSNMP->setHeaderLabels(headers); //设置表头
    treeWidgetSNMP->setColumnCount(2); //设置一共有几列
    treeWidgetSNMP->hideColumn(1);  //隐藏第一列,包括列的头
    treeWidgetSNMP->setMaximumWidth(250); //设置treewidget宽度
treeWidgetSNMP->header()->setMaximumWidth(1); //设置列的宽度
好了,现在在树里添加枝
QStringList iso1;
    iso1 << QObject::tr("iso.org.dod.internet.mgmt.mib-2") << tr(".1.3.6.1.2.1");
    QTreeWidgetItem *iso2 = new QTreeWidgetItem(treeWidgetSNMP, iso1);  //这里第一个参数是他的父,第二个是他显示的名字
    iso2->setData(0, 32, tr(".1.3.6.1.2.1")); //这个比较重要,设置可用的数据,32代表userdata
    iso2->setIcon(0, QIcon(QObject::tr(":/pushButton/2.JPG"))); //设置图标
    iso2->setToolTip(0, tr(".1.3.6.1.2.1"));  //设置鼠标指向时的提示
    connect(treeWidgetSNMP,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(treeIsoInt()));  //当被点击的时候,调用槽函数
  好,如果想在我刚才新建的枝上在建立一个枝,用同样的方法    QStringList system1;
    system1 << QObject::tr("system") << tr(".1.3.6.1.2.1.1");
    QTreeWidgetItem *system2 = new QTreeWidgetItem(iso2, system1);
    system2->setIcon(0, QIcon(QObject::tr(":/pushButton/2.JPG")));
    system2->setData(0, 32, tr(".1.3.6.1.2.1.1"));
    system2->setToolTip(0,tr(".1.3.6.1.2.1.1"));下面贴一张图片
6、treewidget使用
在mainwidget类里定义
QTableWidget *tableWidgetAttr;
在mainwidget构造函数里
tableWidget = new QTableWidget(this);
    tableWidget->setColumnCount(4);  //设置列数
    tableWidget->setRowCount(50);    //设置行数    tableWidget->setShowGrid(1);     //显示栅格
    tableWidget->horizontalHeader()->setStretchLastSection(1); //设置水平方向的最后一列填充整个tablewidget区域
    tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择只选择一行,这里选择可以添加
    for(int i = 0; i < 50; i++)
    {
        tableWidget->setRowHeight(i, 20);   //设置每一行的高度
    }
    tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不允许编译
    tableWidget->verticalHeader()->hide();  //设置隐藏垂直方向的头
  //  tableWidget->set
    QStringList headerslabel;
    headerslabel << tr("Name/OID") << tr("Value") << tr("Type") << tr("IP:Port");
    tableWidget->setHorizontalHeaderLabels(headerslabel);//设置水平方向上的头
tableWidget->setRowHidden(1, 1); //隐藏某一行
QTableWidgetItem *table1 = new QTableWidgetItem(); //以下为设置一个单元格
    table1->setText(tr("Mr zhang"));
    tableWidget->setItem(0, 0, table1);
QLabel *label1 = new QLabel(this); //以下为设置一个单元格用label标签来填充,label上可以显示图片
    label1->setText(tr("TUPIAN"));
    tableWidget->setCellWidget(0, 1, label1);下面贴一下图

7、关于comboBox的使用
在mainwidget类里添加
void INT_HANDLE(int a);
QComboBox *comboBox;
在mainwidget构造函数里添加
comboBox = new QComboBox(this);
    comboBox->addItem(tr("Get Next"));
    comboBox->addItem(tr("Get"));
    comboBox->addItem(tr("Get Bulk"));
    comboBox->addItem(tr("Get Sub Tree"));
    comboBox->addItem(tr("Walk"));
    comboBox->addItem(tr("Set"));
connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(INT_HANDLE(int )));//这里的槽函数一定要注意
下面贴一下图片

8、qpushbutton
pushButtonCreateVideo = new QPushButton;
pushButtonGO->setStyleSheet("QPushButton{color:red;background:blue}");
pushButtonGO->setMenu(); //设置菜单
    pushButtonCreateVideo->setText(tr("Create Video"));
要想显示的文字换行,加\n 就可以了
设置大小
    pushButtonGO->setFixedSize(75,75);
数字转字符显示
    pushButtonGO->setText(QString::number(i-11, 10) + tr("#清水泵\\n停止"));
设置显示内容字体大小
    QFont font("123",30);
    pushButtonGO->setFont(font);

    connect(pushButtonCreateVideo, SIGNAL(clicked()), this, SLOT(pushButtonCreateVideoInt()));
9、qlabel
labelConnectStatus = new QLabel(tr("No connection"));
    labelConnectStatus->setMaximumSize(80, 20);
    labelConnectStatus->setAlignment(Qt::AlignCenter);  //这里设置内容对齐方式
    Lable控件上显示图片
    方法一
    TitleProfile = new QLabel;
    TitleProfile->setPixmap(QPixmap(tr(":/Picture/ProfleYanli.png")));  //这里必须为斜杠,而且图片后缀名必须为png,如为jpg不行
    方法二
    QPixmap TitleProfilePixmap(tr(":/Picture/ProfleYanli.png"));
    TitleProfile->setPixmap(TitleProfilePixmap);

    TitleLable = new QLabel(tr("hello baby!"));
    TitleLable->setAlignment(Qt::AlignCenter);  //设置标签文字对齐方式
    TitleLable->setMinimumSize(500,10);
    TitleLable->hide();//隐藏标签
    TitleLable->show();//显示标签
设置字体颜色
    方法一
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::red);
    TitleLable->setPalette(pe);
    方法二
    QStyle

10、qlineEdit
lineEditHost = new QLineEdit;
11、QTimer
    LabelTimer = new QTimer;
    connect(LabelTimer,SIGNAL(timeout()),this,SLOT(LabelTimerOut()));
    LabelTimer->start(1000);
12、QTime类,
    TitleTime = new QTime;
    TitleLable->setText(TitleTime->currentTime().toString());
    QDate
    TitleDate = new QDate;
    TitleLable->setText(TitleTime->currentTime().toString() + tr(" ") + TitleDate->currentDate().toString());