qt + html实现嵌入文本框和下拉框的表格

因工作需要,我要基于qt做一个表格。表格中一个单元格含有文本框,可以接受用户的输入;另一个单元格是下拉框,其内容随着文本框的输入而变化。

一开始打算用代理(delegate)实现,尝试之后感到比较困难,于是转向qml,也不容易。最后采用的办法是qt + html,原因是html的表格等控件操作比较简单。

下面是代码:

QT       += core gui webkitwidgets network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = htmlTable
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h


RESOURCES += \
    html.qrc

html:






        







1 2
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 



class MainWindow : public QWidget
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    QWebView    *mp_HTML;

public slots:
    void        OnClickLookUp(void);
    void        OnLoadFinished(bool);
    void        OnConfirm(void);
    void        OnRecvBoatNum(QString);
    void        OnRecvSelectedNation(QString);
private:

};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
#include "mainwindow.h"
#include 
#include 
#include 
/********参考网页:
 * https://www.cnblogs.com/guxuanqing/p/4753460.html
 * https://www.cnblogs.com/gcgc/p/11739324.html
 * http://www.qtcn.org/bbs/read-htm-tid-65653.html
 * https://www.cnblogs.com/xiaoleiel/p/8308768.html
 * https://www.cnblogs.com/ivan5277/p/10119144.html
 * https://www.jb51.net/article/107876.htm
 * https://www.cnblogs.com/sheldormhh/p/10302695.html
 * *************/

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent)
{

    QVBoxLayout * pLayout = new QVBoxLayout(this);
    mp_HTML = new QWebView;
    mp_HTML->page()->setForwardUnsupportedContent(true);

    mp_HTML->page()->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
    mp_HTML->settings()->setAttribute(QWebSettings::AutoLoadImages, true);

    connect(mp_HTML, SIGNAL(loadFinished(bool)), this, SLOT(OnLoadFinished(bool)));
    mp_HTML->load(QUrl("qrc:/table.html"));
    pLayout->addWidget(mp_HTML);

    QPushButton * pBtn = new QPushButton("LookUp");
    QHBoxLayout * pHLayout = new QHBoxLayout;
    pHLayout->addStretch(4);
    pHLayout->addWidget(pBtn, 1);
    pLayout->addLayout(pHLayout);

    QPushButton * pBtnConfirm = new QPushButton("Confirm");
    pHLayout->addWidget(pBtnConfirm, 1);

    connect(pBtn, SIGNAL(clicked()), this, SLOT(OnClickLookUp()));
    connect(pBtnConfirm, SIGNAL(clicked()), this, SLOT(OnConfirm()));
}

MainWindow::~MainWindow()
{

}


void MainWindow::OnClickLookUp(void)
{
    QString strVal = QString("Query();");
    mp_HTML->page()->mainFrame()->evaluateJavaScript(strVal);
}

//mp_HTML载入了html脚本后,才能向html注册,在html中的注册名是mywebkit
void MainWindow::OnLoadFinished(bool bFinnish)
{
    if(bFinnish)
    {
        mp_HTML->page()->mainFrame()->addToJavaScriptWindowObject(QString("mywebkit"),this);
    }
}

void MainWindow::OnConfirm(void)
{
    QString strVal = QString("GetNationSelection();");
    mp_HTML->page()->mainFrame()->evaluateJavaScript(strVal);
}

void MainWindow::OnRecvBoatNum(QString qstrNum)
{
    QString strVal = QString("SetNationalities(\"%1\");").arg(qstrNum + qstrNum);
    mp_HTML->page()->mainFrame()->evaluateJavaScript(strVal);
}
void MainWindow::OnRecvSelectedNation(QString qstrNation)
{
    qDebug()<

效果:

在文本框输入内容后,点击LookUp按钮,下拉框被清空,取而代之的是文本框里的文字x2。用户点击Confirm按钮,C++程序读取下拉框的选中内容,用qDebug显示出来(x2)

qt + html实现嵌入文本框和下拉框的表格_第1张图片

你可能感兴趣的:(qt,html)