【Qt開發】QTableWidget設置根據內容調整列寬和行高

QTableWidget要調整表格行寬主要涉及以下一個函數
1.resizeColumnsToContents();??????? ? ??????????? 根據內容調整列寬???????????
2.resizeColumnToContents(int col);?????????????? 根據內容自動調整給定列寬
3.horizontalHeader()->setResizeMode?????????? 把給定列設置為給定模式
主要模式有Stretch和Fixed

  posted @ 2011-11-29 22:21 ccsdu2009 閱讀(486) | 評論 (0) |?編輯?收藏

  QT學習筆記-38.使用QSetting

  QSetting類是QT中專門用于讀寫程序數據的對象
一個簡單的使用例子如下:

  QSettings?setting("config.ini",QSettings::IniFormat);
????setting.beginGroup("config");
???????setting.setValue("page",QVariant(3));
???????setting.setValue("site",QVariant(";));
???????setting.setValue("maker",QVariant("Gaimor"));
????setting.endGroup();

這個例子是把數據寫到配置文件config.ini中去
當然也可以使用分組的方式寫入,具體如下:

  setting.setValue("config/page",QVariant(3));

  setting.setValue("config/site",QVariant(";));

  setting.setValue("config/maker",QVariant("Gaimor"));


它的讀寫值得函數原型如下:

  void?setValue(const?QString?&key,?const?QVariant?&value);
????QVariant?value(const?QString?&key,?const?QVariant?&defaultValue?=?QVariant())?const;


當然QSetting還有其他使用方式,以后慢慢總結

  posted @ 2011-11-29 21:56 ccsdu2009 閱讀(250) | 評論 (0) |?編輯?收藏

  QT學習筆記-35:QTableWidget動態增加行的方法

  QTableWidget動態增加行的方法
其實很簡單的:
首先rowCount()得到當前中的行數,然后在調用insertRow(row);即可
如果要動態增加列,方法類似

  posted @ 2011-11-27 11:49 ccsdu2009 閱讀(355) | 評論 (0) |?編輯?收藏

  QT學習筆記-34.QT中Pro文件的寫法

  QT中 .pro文件的寫法如下:

  1.?注釋
從“#”開始,到這一行結束。

  2.?指定源文件
SOURCES = *.cpp

  對于多源文件,可用空格分開,如:SOURCES = 1.cpp 2.cpp3.cpp
或者每一個文件可以被列在一個分開的行里面,通過反斜線另起一行,就像這樣:

  SOURCES = hello.cpp \
????????????????? main.cpp
還有一種是這樣

  SOURCES+= hello.cpp
??? SOURCES +=main.cpp

  這種方法中使用“+=”比“=”更安全,因為它只是向已有的列表中添加新的文件,而不是替換整個列表。

  3.?指定頭文件
HEADERS = hello.h或者HEADERS += hello.h

  列出源文件的任何一個方法對頭文件也都適用。

  4.?配置信息
CONFIG用來告訴qmake關于應用程序的配置信息。

  CONFIG+= qt warn_on release

  在這里使用“+=”,是因為我們添加我們的配置選項到任何一個已經存在中。這樣做比使用“=”那樣替換已經指定的所有選項是更安全的。
A$amp;>amp;$nbsp;qt部分告訴qmake這個應用程序是使用Qt來連編的。這也就是說qmake在連接和為編譯添加所需的包含路徑的時候會考慮到Qt庫的。
B$amp;>amp;$nbsp;warn_on部分告訴qmake要把編譯器設置為輸出警告信息的。
C$amp;>amp;$nbsp;release部分告訴qmake應用程序必須被連編為一個發布的應用程序。在開發過程中,程序員也可以使用debug來替換release

  5.?指定目標文件名
TARGET = filename

  如果不設置該項目,目標名會被自動設置為跟項目文件一樣的名稱

  6.?添加界面文件(ui)
INTERFACES = filename.ui

  7.?平臺相關性處理
我們在這里需要做的是根據qmake所運行的平臺來使用相應的作用域來進行處理。為Windows平臺添加的依賴平臺的文件的簡單的作用域看起來就像這樣:

  win32 {
SOURCES += hello_win.cpp
}

  所以如果qmake運行在Windows上的時候,它就會把hello_win.cpp添加到源文件列表中。如果qmake運行在其它平臺上的時候,它會很簡單地把這部分忽略。

  8.?如果一個文件不存在,停止qmake
如果某一個文件不存在的 時候,你也許不想生成一個Makefile。我們可以通過使用exists()函數來檢查一個文件是否存在。我們可以通過使用error()函數把正在運 行的qmake停下來。這和作用域的工作方式一樣。只要很簡單地用這個函數來替換作用域條件。對main.cpp文件的檢查就像這樣:

  !exists( main.cpp ) {
??error( "No main.cpp file found")
}

  “!”用來否定這個測試,比如,如果文件存在,exists( main.cpp)是真,如果文件不存在,!exists( main.cpp )是真。

  9.?檢查多于一個的條件
假設你使用Windows并且當你在命令 行運行你的應用程序的時候你想能夠看到qDebug()語句。除非你在連編你的程序的時候使用console設置,你不會看到輸出。我們可以很容易地把 console添加到CONFIG行中,這樣在Windows下,Makefile就會有這個設置。但是如果告訴你我們只是想在當我們的應用程序運行在 Windows下并且當debug已經在CONFIG行中的時候,添加console。這需要兩個嵌套的作用域;只要生成一個作用域,然后在它里面再生成 另一個。把設置放在最里面的作用域里,就像這樣:

  win32 {
??debug {
?????CONFIG += console
??}
}

  嵌套的作用域可以使用冒號連接起來,像這樣:

  win32:debug {
CONFIG += console
}

  10.?摸板
模板變量告訴qmake為這個應用程序生成哪種makefile。下面是可供使用的選擇:

  A$amp;>amp;$nbsp;app -建立一個應用程序的makefile。這是默認值,所以如果模板沒有被指定,這個將被使用。
B$amp;>amp;$nbsp;lib - 建立一個庫的makefile。
C$amp;>amp;$nbsp;vcapp - 建立一個應用程序的VisualStudio項目文件。
D$amp;>amp;$nbsp;vclib - 建立一個庫的VisualStudio項目文件。
E$amp;>amp;$nbsp;subdirs -這是一個特殊的模板,它可以創建一個能夠進入特定目錄并且為一個項目文件生成makefile并且為它調用make的makefile。

  11.?生成Makefile
當你已經創建好你的項目文件,生成Makefile就很容易了,你所要做的就是先到你所生成的項目文件那里然后輸入:

  Makefile可以像這樣由“.pro”文件生成:

  qmake -oMakefile hello.pro

  對于VisualStudio的用戶,qmake也可以生成“.dsp”文件,例如:

??? qmake -tvcapp -o hello.dsp hello.pro

  posted @ 2011-11-26 21:50 ccsdu2009 閱讀(152) | 評論 (0) |?編輯?收藏

  QT學習筆記-33.QTableView增加QCheckBox的方法

  使用Delegate具體如下:

  class?QLineDelegate?:?public?QStyledItemDelegate
{
????Q_OBJECT
public:
????QLineDelegate(QTableView*?tableView);
protected:
????void?paint(QPainter*?painter,const?QStyleOptionViewItem&?option,const?QModelIndex&?index)?const;
????bool?editorEvent(QEvent?*event,
?????????????????????????????????????????QAbstractItemModel?*model,
?????????????????????????????????????????const?QStyleOptionViewItem?&option,
?????????????????????????????????????????const?QModelIndex?&index)?;
private:
????QPen?????????pen;
????QTableView*??view;
};


  static?QRect?CheckBoxRect(const?QStyleOptionViewItem?&view_item_style_options)
{
????QStyleOptionButton?check_box_style_option;
????QRect?check_box_rect?=?QApplication::style()->subElementRect(
????????QStyle::SE_CheckBoxIndicator,
????????&check_box_style_option);

????QPoint?check_box_point(view_item_style_options.rect.x()?+
???????????????????????????view_item_style_options.rect.width()?/?2?-
???????????????????????????check_box_rect.width()?/?2,
???????????????????????????view_item_style_options.rect.y()?+
???????????????????????????view_item_style_options.rect.height()?/?2?-
???????????????????????????check_box_rect.height()?/?2);
????return?QRect(check_box_point,?check_box_rect.size());
}


QLineDelegate::QLineDelegate(QTableView*?tableView)
{
????int?gridHint?=?tableView->style()->styleHint(QStyle::SH_Table_GridLineColor,?new?QStyleOptionViewItemV4());
????QColor?gridColor?=?static_cast<QRgb>(gridHint);
????pen?=?QPen(gridColor,0,tableView->gridStyle());
????view?=?tableView;
}

void?QLineDelegate::paint(QPainter*?painter,?const?QStyleOptionViewItem&?option,const?QModelIndex&?index)const
{
????bool?checked?=?index.model()->data(index,?Qt::DisplayRole).toBool();

????if(index.column()?==?0)
????{
????????QStyleOptionButton?check_box_style_option;
????????check_box_style_option.state?|=?QStyle::State_Enabled;
????????if(checked)
????????{
????????????check_box_style_option.state?|=?QStyle::State_On;
????????}
????????else
????????{
????????????check_box_style_option.state?|=?QStyle::State_Off;
????????}
????????check_box_style_option.rect?=?CheckBoxRect(option);
????????QApplication::style()->drawControl(QStyle::CE_CheckBox,&check_box_style_option,painter);
????}

????QStyledItemDelegate::paint(painter,option,index);
????QStyleOptionViewItem?itemOption(option);
????if(itemOption.state?&?QStyle::State_HasFocus)
????????itemOption.state?=?itemOption.state?^?QStyle::State_HasFocus;
????QStyledItemDelegate::paint(painter,itemOption,index);
????QPen?oldPen?=?painter->pen();
????painter->setPen(pen);
????painter->drawLine(option.rect.topRight(),option.rect.bottomRight());
????painter->drawLine(itemOption.rect.bottomLeft(),itemOption.rect.bottomRight());
????painter->setPen(oldPen);
}

bool?QLineDelegate::editorEvent(QEvent?*event,
???????????????????????????????????QAbstractItemModel?*model,
???????????????????????????????????const?QStyleOptionViewItem?&option,
???????????????????????????????????const?QModelIndex?&index)?{
????if((event->type()?==?QEvent::MouseButtonRelease)?||
????????(event->type()?==?QEvent::MouseButtonDblClick))
????{
????????QMouseEvent?*mouse_event?=?static_cast<QMouseEvent*>(event);
????????if(mouse_event->button()?!=?Qt::LeftButton?||
???????????!CheckBoxRect(option).contains(mouse_event->pos()))
????????{
????????????return?false;
????????}

????????if(event->type()?==?QEvent::MouseButtonDblClick)
????????{
????????????return?true;
????????}
????}
????else?if(event->type()?==?QEvent::KeyPress)
????{
????????if(static_cast<QKeyEvent*>(event)->key()?!=?Qt::Key_Space?&&
???????????static_cast<QKeyEvent*>(event)->key()?!=?Qt::Key_Select)
????????{
????????????return?false;
????????}
????}
????else
????{
????????return?false;
????}

????bool?checked?=?index.model()->data(index,?Qt::DisplayRole).toBool();
????return?model->setData(index,?!checked,?Qt::EditRole);
}

不過有一個小問題,就是CheckBox旁邊有false/true字符

  posted @ 2011-11-26 12:33 ccsdu2009 閱讀(461) | 評論 (0) |?編輯?收藏

  QT學習筆記-32.QTableWidget增加QCheckBox的方法

  #include?<QtGui>

class?ItemDelegate?:?public?QItemDelegate
{
public:
????ItemDelegate(QObject?*parent?=?0)
????????:?QItemDelegate(parent)
????{
????}
????virtual?void?drawCheck(QPainter?*painter,?const?QStyleOptionViewItem?&option,
????????const?QRect?&,?Qt::CheckState?state)const
????{
????????const?int?textMargin?=?QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin)?+?1;

????????QRect?checkRect?=?QStyle::alignedRect(option.direction,?Qt::AlignCenter,
????????????check(option,?option.rect,?Qt::Checked).size(),
????????????QRect(option.rect.x()?+?textMargin,?option.rect.y(),
????????????option.rect.width()?-?(textMargin?*?2),?option.rect.height()));
????????QItemDelegate::drawCheck(painter,?option,?checkRect,?state);
????}
????virtual?bool?editorEvent(QEvent?*event,?QAbstractItemModel?*model,?const?QStyleOptionViewItem?&option,
????????const?QModelIndex?&index)
????{
????????Q_ASSERT(event);
????????Q_ASSERT(model);

????????Qt::ItemFlags?flags?=?model->flags(index);
????????if(!(flags?&?Qt::ItemIsUserCheckable)?||?!(flags?&?Qt::ItemIsEnabled))
????????????return?false;

????????QVariant?value?=?index.data(Qt::CheckStateRole);
????????if(!value.isValid())
????????????return?false;

????????if(event->type()?==?QEvent::MouseButtonRelease)
????????{
????????????const?int?textMargin?=?QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin)?+?1;
????????????QRect?checkRect?=?QStyle::alignedRect(option.direction,?Qt::AlignCenter,
????????????????check(option,?option.rect,?Qt::Checked).size(),
????????????????QRect(option.rect.x()?+?textMargin,?option.rect.y(),
????????????????option.rect.width()?-?(2?*?textMargin),?option.rect.height()));
????????????if?(!checkRect.contains(static_cast<QMouseEvent*>(event)->pos()))
????????????????return?false;
????????}
????????else?if(event->type()?==?QEvent::KeyPress)
????????{
????????????if(static_cast<QKeyEvent*>(event)->key()?!=?Qt::Key_Space
????????????????&&?static_cast<QKeyEvent*>(event)->key()?!=?Qt::Key_Select)
????????????????return?false;
????????}
????????else
????????{
????????????return?false;
????????}
????????Qt::CheckState?state?=?(static_cast<Qt::CheckState>(value.toInt())?==?Qt::Checked
???????????? ?Qt::Unchecked?:?Qt::Checked);

????????//!?15x20

????????return?model->setData(index,?state,?Qt::CheckStateRole);
????}
????void?drawFocus(QPainter?*painter,?const?QStyleOptionViewItem?&option,?const?QRect?&rect)?const
????{
????????QItemDelegate::drawFocus(painter,?option,?option.rect);
????}
};

static?int?ROWS?=?3;
static?int?COLS?=?3;

class?Table?:?public?QTableWidget
{
public:
????Table(QWidget?*parent?=?0)
????????:?QTableWidget(ROWS,?COLS,?parent)
????{
????????setItemDelegate(new?ItemDelegate(this));
????????QTableWidgetItem?*item?=?0;
????????for(int?i=0;?i<rowCount();?++i)
????????{
????????????for(int?j=0;?j<columnCount();?++j)
????????????{
????????????????setItem(i,?j,?item?=?new?QTableWidgetItem);
????????????????QTableViewItem;
????????????????item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsUserCheckable);
????????????????item->setCheckState((i+j)?%?2?==?0? ?Qt::Checked?:?Qt::Unchecked);
????????????}
????????}
????}
};

int?main(int?argc,?char?**argv)
{
????QApplication?a(argc,?argv);
????Table?w;
????w.show();
????return?a.exec();
}

  posted @ 2011-11-26 11:25 ccsdu2009 閱讀(264) | 評論 (0) |?編輯?收藏

  QT學習筆記-31.QTableView只顯示橫格,不顯示點擊虛框的方法

  重新風格項代理QStyledItemDelegat

  class?QLineDelegate?:?public?QStyledItemDelegate
{
????Q_OBJECT
public:
????QLineDelegate(QTableView*?tableView);
protected:
????void?paint(QPainter*?painter,const?QStyleOptionViewItem&?option,const?QModelIndex&?index)?const;
private:
????QPen?????????pen;
????QTableView*??view;
};


  #include?<QPainter>
#include?"QLineDelegate.h"

QLineDelegate::QLineDelegate(QTableView*?tableView)
{
????int?gridHint?=?tableView->style()->styleHint(QStyle::SH_Table_GridLineColor,?new?QStyleOptionViewItemV4());
????QColor?gridColor?=?static_cast<QRgb>(gridHint);
????pen?=?QPen(gridColor,?0,?tableView->gridStyle());
????view?=?tableView;
}

void?QLineDelegate::paint(QPainter*?painter,?const?QStyleOptionViewItem&?option,const?QModelIndex&?index)const
{
????QStyleOptionViewItem?itemOption(option);
????if(itemOption.state?&?QStyle::State_HasFocus)
????????itemOption.state?=?itemOption.state?^?QStyle::State_HasFocus;
????QStyledItemDelegate::paint(painter,itemOption,index);
????QPen?oldPen?=?painter->pen();
????painter->setPen(pen);
????//painter->drawLine(option.rect.topRight(),option.rect.bottomRight());
????painter->drawLine(itemOption.rect.bottomLeft(),itemOption.rect.bottomRight());
????painter->setPen(oldPen);
}

  posted @ 2011-11-25 20:22 ccsdu2009 閱讀(228) | 評論 (0) |?編輯?收藏

  QT學習筆記-30.QTableView只顯示橫格的方式

  默認QTableView是顯示網狀格子的,如果不顯示網格則可以調用setShowGrid(false);來實現。但是如果只想顯示橫線則有點復雜了。具體代碼如下:

  #include <QApplication>

  #include <QTableWidget>

  #include <QPainter>

  #include <QStyledItemDelegate>

  #include <QHeaderView>

  class QLineDelegate : public QStyledItemDelegate

  {

  public:

  QLineDelegate(QTableView* tableView);

  protected:

  void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;

  private:

  QPen pen;

  QTableView* view;

  };

  QLineDelegate::QLineDelegate(QTableView* tableView)

  {

  int gridHint = tableView->style()->styleHint(QStyle::SH_Table_GridLineColor, new QStyleOptionViewItemV4());

  QColor gridColor = static_cast<QRgb>(gridHint);

  pen = QPen(gridColor, 0, tableView->gridStyle());

  view = tableView;

  }

  void QLineDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,const QModelIndex& index)const

  {

  QStyledItemDelegate::paint(painter, option, index);

  QPen oldPen = painter->pen();

  painter->setPen(pen);

  //painter->drawLine(option.rect.topRight(), option.rect.bottomRight());

  painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());

  painter->setPen(oldPen);

  }

  class QLineTableWidget:public QTableWidget

  {

  public:

  QLineTableWidget();

  };

  QLineTableWidget::QLineTableWidget()

  {

  setStyleSheet("QTableView::Item{selection-background-color:#101020}");

  setStyleSheet("QTableView::Item{background-color:#F0F0F0}");

  verticalHeader()->setVisible(false);

  horizontalHeader()->setVisible(true);

  setSelectionBehavior(QAbstractItemView::SelectRows);

  setSelectionMode(QAbstractItemView::SingleSelection);

  setEditTriggers(QTableView::NoEditTriggers);

  setColumnCount(3);

  setRowCount(4);

  setShowGrid(false);

  setItemDelegate(new QLineDelegate(this));

  setCurrentCell(-1,-1);

  }

  int main(int argc,char **argv)

  {

  QApplication a(argc,argv);

  QLineTableWidget widget;

  widget.show();

  return a.exec();

  }

  posted @ 2011-11-22 22:23 ccsdu2009 閱讀(1253) | 評論 (0) |?編輯?收藏

  QT學習筆記-30.自寫的QT插件系統

  #ifndef?QPLUGIN_SYSTEM_H
#define?QPLUGIN_SYSTEM_H
#include?<QObject>
#include?<QVector>
#include?<QPluginLoader>
#include?<QDir>

template<class?T>
class?QPluginSystem
{
public:
????void?setup();
????int??getAddonCnt(){return?addons.size();}
????T*???getAddonByIndex(int?index){return?addons.at(index);}????
private:
????QVector<QPluginLoader*$amp;>amp;$nbsp;loaders;
????QVector<T*$amp;>amp;$nbsp;????????????addons;
};

template<class?T>
void?QPluginSystem<T>::setup()
{
????QString?path?=?QDir::currentPath();
????path?+=?QDir::separator();
????path?+=?"addons";
????QDir?pluginsDir(path);

????foreach(QString?fileName,pluginsDir.entryList(QDir::Files))
????{???
????????bool?autodel?=?false;
????????QPluginLoader*?pluginLoader?=?new?QPluginLoader(pluginsDir.absoluteFilePath(fileName));
????????QObject?*plugin?=?pluginLoader->instance();
????????if(plugin)
????????{
????????????T?*interface?=?qobject_cast<T*>(plugin);
????????????if(interface)
????????????{
????????????????addons.push_back(interface);
????????????????loaders.push_back(pluginLoader);
????????????????autodel?=?true;
????????????}
????????}

????????if(autodel?==?false)
????????????delete?pluginLoader;
????}
}

#endif
其中T是插件對象,需要繼承QObject
另外雖然我沒試過,但是我感覺增加QPluginLoader鏈表是很有必要的

  posted @ 2011-11-17 22:11 ccsdu2009 閱讀(121) | 評論 (0) |?編輯?收藏

  QT學習筆記-29.使用QT HTTP下載網絡文件

QT附帶的例子比較好:

  class?HttpWindow?:?public?QDialog
{
????Q_OBJECT
public:
????HttpWindow(QWidget?*parent?=?0);

????void?startRequest(QUrl?url);
private?slots:
????void?downloadFile();
????void?cancelDownload();
????void?httpFinished();
????void?httpReadyRead();
????void?updateDataReadProgress(qint64?bytesRead,?qint64?totalBytes);
????void?enableDownloadButton();
????void?slotAuthenticationRequired(QNetworkReply*,QAuthenticator?*);
private:
????QLabel?*statusLabel;
????QLabel?*urlLabel;
????QLineEdit?*urlLineEdit;
????QProgressDialog?*progressDialog;
????QPushButton?*downloadButton;
????QPushButton?*quitButton;
????QDialogButtonBox?*buttonBox;

????QUrl?url;
????QNetworkAccessManager?qnam;
????QNetworkReply?*reply;
????QFile?*file;
????int?httpGetId;
????bool?httpRequestAborted;
};

其中槽有:
1.開始下載
2.取消下載
3.預備下載
4.下載完成
5.進度回調


實現為:

  void?HttpWindow::startRequest(QUrl?url)
{
????reply?=?qnam.get(QNetworkRequest(url));
????connect(reply,?SIGNAL(finished()),
????????????this,?SLOT(httpFinished()));
????connect(reply,?SIGNAL(readyRead()),
????????????this,?SLOT(httpReadyRead()));
????connect(reply,?SIGNAL(downloadProgress(qint64,qint64)),
????????????this,?SLOT(updateDataReadProgress(qint64,qint64)));
}

該函數主要針對給定url綁定事件

  void?HttpWindow::downloadFile()
{
????url?=?urlLineEdit->text();

????QFileInfo?fileInfo(url.path());
????QString?fileName?=?fileInfo.fileName();
????fileName?=?"downloadfile.dat";
????if(fileName.isEmpty())
????????fileName?=?"index.html";

????if(QFile::exists(fileName))?{
????????if?(QMessageBox::question(this,?tr("HTTP"),?
??????????????????????????????????tr("There?already?exists?a?file?called?%1?in?"
?????????????????????????????????????"the?current?directory.?Overwrite ").arg(fileName),
??????????????????????????????????QMessageBox::Yes|QMessageBox::No,?QMessageBox::No)
????????????==?QMessageBox::No)
????????????return;
????????QFile::remove(fileName);
????}

????file?=?new?QFile(fileName);
????if?(!file->open(QIODevice::WriteOnly))?{
????????QMessageBox::information(this,?tr("HTTP"),
?????????????????????????????????tr("Unable?to?save?the?file?%1:?%2.")
?????????????????????????????????.arg(fileName).arg(file->errorString()));
????????delete?file;
????????file?=?0;
????????return;
????}

????progressDialog->setWindowTitle(tr("HTTP"));
????progressDialog->setLabelText(tr("Downloading?%1.").arg(fileName));
????downloadButton->setEnabled(false);

????//?schedule?the?request
????httpRequestAborted?=?false;
????startRequest(url);
}

當點擊下載的時候,會執行該函數
獲取url鏈接,生成本地文件,...

  void?HttpWindow::cancelDownload()
{
????statusLabel->setText(tr("Download?canceled."));
????httpRequestAborted?=?true;
????reply->abort();
????downloadButton->setEnabled(true);
}

終止下載,主要函數是reply->abort();

  void?HttpWindow::httpFinished()
{
????if?(httpRequestAborted)?{
????????if?(file)?{
????????????file->close();
????????????file->remove();
????????????delete?file;
????????????file?=?0;
????????}
????????reply->deleteLater();
????????progressDialog->hide();
????????return;
????}

????progressDialog->hide();
????file->flush();
????file->close();


????QVariant?redirectionTarget?=?reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
????if?(reply->error())?{
????????file->remove();
????????QMessageBox::information(this,?tr("HTTP"),
?????????????????????????????????tr("Download?failed:?%1.")
?????????????????????????????????.arg(reply->errorString()));
????????downloadButton->setEnabled(true);
????}?else?if?(!redirectionTarget.isNull())?{????????
????????QUrl?newUrl?=?url.resolved(redirectionTarget.toUrl());
????????if?(QMessageBox::question(this,?tr("HTTP"),
??????????????????????????????????tr("Redirect?to?%1? ").arg(newUrl.toString()),
??????????????????????????????????QMessageBox::Yes?|?QMessageBox::No)?==?QMessageBox::Yes)?{
????????????url?=?newUrl;
????????????reply->deleteLater();
????????????file->open(QIODevice::WriteOnly);
????????????file->resize(0);
????????????startRequest(url);
????????????return;
????????}
????}?else?{
????????QString?fileName?=?QFileInfo(QUrl(urlLineEdit->text()).path()).fileName();
????????statusLabel->setText(tr("Downloaded?%1?to?current?directory.").arg(fileName));
????????downloadButton->setEnabled(true);
????}

????reply->deleteLater();
????reply?=?0;
????delete?file;
????file?=?0;
}

? 下載結束動作

  void?HttpWindow::httpReadyRead()
{
????//?this?slot?gets?called?every?time?the?QNetworkReply?has?new?data.
????//?We?read?all?of?its?new?data?and?write?it?into?the?file.
????//?That?way?we?use?less?RAM?than?when?reading?it?at?the?finished()
????//?signal?of?the?QNetworkReply
????if?(file)
????????file->write(reply->readAll());
}

寫文件回調

  void?HttpWindow::updateDataReadProgress(qint64?bytesRead,?qint64?totalBytes)
{
????if?(httpRequestAborted)
????????return;

????progressDialog->setMaximum(totalBytes);
????progressDialog->setValue(bytesRead);
}

轉載于:https://www.cnblogs.com/huty/p/8518171.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/457307.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/457307.shtml
英文地址,請注明出處:http://en.pswp.cn/news/457307.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

深入淺出mysql數據開發_深入淺出MySQL數據庫開發、優化與管理維護 PDF掃描版[513KB]...

深入淺出MySQL數據庫開發、優化與管理維護 內容介紹&#xff1a;本書從數據庫的基礎、開發、優化、管理維護4個方面對MySQL進行了詳細的介紹&#xff0c;其中每一部分都獨立成篇。本書內容實用&#xff0c;覆蓋廣泛&#xff0c;講解由淺入深&#xff0c;適合于各個層次的讀者。…

Understand Lambda Expressions in 3 minutes(翻譯)

本文翻譯自CodeProject上的一篇簡單解釋Lambda表達式的文章&#xff0c;適合新手理解。譯文后面我補充了一點對Lambda表達式的說明。 1.什么是Lambda表達式&#xff1f; Lambda表達式是一種匿名方法&#xff0c;多數情況下用來在LINQ中快速創建委托。簡單地說&#xff0c;它代表…

Hibernate二級緩存配置

一、定義&#xff1a; 二級緩存是進程或集群范圍內的緩存&#xff0c;可以被所有的Session共享&#xff0c;是可配置的插件 二、二級緩存原理圖 解析&#xff1a;每次從二級緩存中取出的對象&#xff0c;都是一個新的對象。 三、配置步驟如下&#xff1a; 同理&#xff1a;以員…

redis配置主從沒效果_跟我一起學Redis之加個哨兵讓主從復制更加高可用

Redis哨兵(Sentinel)其實本質就是一個RedisServer節點&#xff0c;通過設置 運行模式 來開啟哨兵的功能&#xff1b;主要功能如下&#xff1a;監控(Monitoring )&#xff1a;哨兵節點會不斷地檢查的主服務和從服務的運行狀態&#xff1b;自動故障遷移(Automatic failover) &…

閏秒導致MySQL服務器的CPU sys過高

今天&#xff0c;有個哥們碰到一個問題&#xff0c;他有一個從庫&#xff0c;只要是啟動MySQL&#xff0c;CPU使用率就非常高&#xff0c;其中sys占比也比較高&#xff0c;具體可見下圖。 注意&#xff1a;他的生產環境是物理機&#xff0c;單個CPU&#xff0c;4個Core。 于是&…

position定位——讓人又愛又恨的屬性

關于css中的position這個屬性&#xff0c;在使用的時候&#xff0c;有時很強大&#xff0c;有時又讓人很無奈。 強大的時候&#xff0c;對于div中的一些小物件不方便使用margin或者padding的時候&#xff0c;給與position:absolute;再配備left、right、top和bottom&#xff0c;…

CentOS 6.8安裝Python2.7.13

轉載自&#xff1a;http://www.cnblogs.com/94YY/p/6224441.html查看當前系統中的 Python 版本python --version返回 Python 2.6.6 為正常。檢查 CentOS 版本cat /etc/redhat-release返回 CentOS release 6.8 (Final) 為正常。安裝所有的開發工具包yum groupinstall -y "D…

新安裝數據庫sqlserver2008r2,使用javaweb連接不上問題處理

鼠標右鍵【計算機】--》【管理】&#xff0c;打開界面如下&#xff1a; 選擇自己數據庫的實例名&#xff1a; 選擇TCP/IP&#xff1a;右鍵【屬性】&#xff0c;將所有TCP動態端口的【0】刪掉&#xff0c;TCP端口設為1433&#xff1b;重啟服務&#xff0c;即可連接。PS:不知道這…

vue 鼠標點擊事件_VBA代碼解決方案第115講:點擊鼠標實現精準控制觸發事件的VBA代碼第二方案...

大家好&#xff0c;我們今日繼續講解VBA代碼解決方案的第115講內容&#xff1a;工作表事件中&#xff0c;根據Target參數不同&#xff0c;實現精準控制觸發事件的VBA代碼第二方案。在上一講中我們講了利用Address的屬性實現控制觸發事件的方案&#xff0c;今日講解第二方案&…

mysql注入5.0以上_[Injection]對MYSQL 5.0服務器以上版本注入

How to do a SQL Injection for MYSQL Server 5.01. Find a vulnerable add a ‘ at the end of the site example: news.php?id1 add a ‘ at the end of the 1 and see if you get a syntax error2. order by #–Keep upping the # until you get an error.3. union all sel…

動態規劃(制表法)模板及應用

int cache[100][100] 初始化為全體為 -1&#xff0c;這樣在 cache 中存儲的可以是其他任意非負整數&#xff0c;也可以是布爾類型 0/1 &#xff08;true/false&#xff09;&#xff0c;1. 模板 int cache[2500][2500];// 初始化為 -1&#xff0c;memset(cache, -1, sizeof(cach…

(翻譯)31天Windows Phone學習-1-項目模板

今天在在外文網站Google關于Windows Phone 7的學習資料&#xff0c;無疑間Google到了Jeff Blankenburg的 31 Days of Windows Phone這個系列&#xff0c;感覺寫的比較基礎和淺顯易懂&#xff0c;適合我這種入們級的人學習&#xff0c;所以準備拿來對Windows Phone 7的簡單入門學…

Mssql 跨域查詢

有數據庫test1和數據庫test2。其中test1中有表 table1、table2&#xff1b;test2 中有表 table1。三個表的字段都為為&#xff1a;id、xingming、shijian、shuliang。接下來我們就以上面的條件為例來介紹跨數據庫查詢和跨表 查詢的方法。 SELECT * FROM OPENROWSET(sqloledb…

arch mysql日志位置_MySQL 日志文件與相關參數

1 、參數文件及mysql參數查看mysql 的 my.cnf 配置文件位置命令&#xff1a;>./bin/mysql --help | grep my.cnf查看mysql 的參數設置命令&#xff1a; mysql > show variables --顯示所有參數; // show variables like log_error% 顯示某匹配參數mysql > select se…

MOSS點滴(2):自定義Application Page

在MOSS中后臺管理的頁面都是Application Page&#xff0c;比如網站設置的頁面(settings.aspx)就是典型的Application Page&#xff0c;它不能被Sharepoint Desiger定制。如果我們要修改只能手動的使用其他工具來修改&#xff0c;我們也可以添加Application Page&#xff0c;必須…

wpf使用webbrowser時提示當前頁面腳本發生錯誤_win7系統internet腳本錯誤的應對辦法...

最近有win7系統用戶反饋&#xff0c;上網時遇到ie瀏覽器總會提示internet腳本錯誤的問題&#xff0c;不知如何解決&#xff0c;為此非常苦惱&#xff0c;那么win7系統提示internet腳本錯誤怎么辦呢?就此問題&#xff0c;下面小編教你win7系統internet腳本錯誤的應對辦法。有不…

JDK的目錄結構及結構圖

-bin目錄: JDK開發工具的可執行文件 -lib目錄: 開發工具使用的歸檔包文件 -jre: Java 運行時環境的根目錄&#xff0c;包含Java虛擬機&#xff0c;運行時的類包和Java應用啟動器&#xff0c; 但不包含開發環境中的開發工具 -demo: 含有源代碼的程序示例 -include: 包含…

java9-1.類,抽象類,接口的綜合小練習

1 /*2 教練和運動員案例(學生分析然后講解)3 乒乓球運動員和籃球運動員。4 乒乓球教練和籃球教練。5 為了出國交流&#xff0c;跟乒乓球相關的人員都需要學習英語。6 請用所學知識&#xff1a;7 分析&#xff0c;這個案例中有哪些抽象類&#xff0c;哪些接…

java發送c語言結構體_C語言中結構體直接賦值?

在C語言中結構體變量之間可以進行賦值操作嗎&#xff1f;簡單結構體的賦值先說結論&#xff1a;一般來說&#xff0c;C語言中的結構體變量可以用另一個變量對其進行賦值或初始化。簡單結構體(不包含指針成員)直接賦值沒有問題。我們先下面一段代碼&#xff1a;#include #includ…

Cacti 使用安裝詳解-企業級實例

Cacti是一套基于PHP,MySQL,SNMP及RRDTool開發的網絡流量監測圖形分析工具。Cacti是通過 snmpget來獲取數據&#xff0c;使用 RRDtool繪畫圖形&#xff0c;它提供了非常強大的數據和用戶管理功能&#xff0c;可以指定每一個用戶能查看樹狀結構、host以及任何一張圖&#xff0c;還…