1. 程式人生 > >QListWidget的item檢測到被選中行為

QListWidget的item檢測到被選中行為

  • 在維護同事的一個程式時,發現一個小bug。具體情形是這樣:有一個對話方塊,使用者可選中QListWidget中的某一項進行開啟刪除操作;在進行刪除操作時,沒有去判斷item是否被選中進執行刪除操作,會引起程式崩潰問題。於是就有了這篇小文。直接上程式碼:

.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget
(QWidget *parent = 0); ~Widget(); private slots: void on_pushButton_open_clicked(); void on_pushButton_delete_clicked(); void setPushButtonEnable(); private: Ui::Widget *ui; void initFileList(); }; #endif // WIDGET_H

.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QFile>
#include <QDebug> #include <QDir> #include <QMessageBox> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); initFileList(); ui->pushButton_delete->setEnabled(false);//初始化刪除按鈕不可點選,當有檔案選中時才可點選 connect(ui->listWidget,SIGNAL(itemSelectionChanged()),SLOT(setPushButtonEnable()));//
重要的槽連線,檢測是否有item改變 } Widget::~Widget() { delete ui; } void Widget::on_pushButton_open_clicked() { if(ui->listWidget->count() != 0) { qDebug() << "open the" << ui->listWidget->currentItem()->text(); } } void Widget::on_pushButton_delete_clicked() { QString runPath = QCoreApplication::applicationDirPath(); if(QMessageBox::warning(this, "Warning", "Delete this file?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) { if(ui->listWidget->count() != 0) { QFile::remove(runPath + "/File/" + ui->listWidget->currentItem()->text()); initFileList(); } else { QMessageBox::warning(this, "Warning", "No file is selected!", QMessageBox::Yes); } } else { return; } } void Widget::setPushButtonEnable() { ui->pushButton_delete->setEnabled(true); } void Widget::initFileList() { QString runPath = QCoreApplication::applicationDirPath(); QDir *dir = new QDir(runPath + "/File"); QStringList filter; qDebug() <<"path:" << runPath + "/File"; filter << "*.txt"; dir->setNameFilters(filter); QList<QFileInfo> *fileInfo = new QList<QFileInfo>(dir->entryInfoList(filter)); ui->listWidget->clear(); for(int i = 0; i < fileInfo->count();i++) { ui->listWidget->addItem(fileInfo->at(i).fileName()); } }

其中要在程式執行目錄下建立File資料夾,才能正常操作。

相關推薦

QListWidget的item檢測選中行為

在維護同事的一個程式時,發現一個小bug。具體情形是這樣:有一個對話方塊,使用者可選中QListWidget中的某一項進行開啟刪除操作;在進行刪除操作時,沒有去判斷item是否被選中進執行刪除操作,會

jQuery檢測判斷複選框是否選中了的幾種方法

今天專案有個新的需求,實現一個恢復出廠設定的功能,後臺引數已經給我,需要我根據引數做出恢復出廠的倆種情況,一種是恢復資料,另一種資料和配置都要恢復。所以我決定用用複選框來控制兩種情況,通過複選框來判斷ajax傳遞的路徑。 方法一: if ($("#checkbox-id"

js判斷checkbox是否選中

value val check ntb checked div tel col cnblogs if(document.getElementById("checkboxID").checked){ alert(document.getElementById("ch

jQuery獲取動態表格中checkbox選中一行的屬性數據

jquery table tr 表格 動態 http://www.jianshu.com/p/0ec66caf4c40 jQuery獲取表格中checkbox被選中一行的屬性數據jQuery獲取動態表格中checkbox被選中一行的屬性數據

select標簽模擬placeholder屬性與一般操作(最重要的是ios某一項選中的兼容)

hid rem itext lac 沒有 最重要的 _id get find 1.為了統一樣式,常常要模擬placeholder <select> <option disabled selected hidden>請選擇</option

獲取JQuery UI tabs中選中的tabs的方法

script 如果 選中 tabs -h gitbook tle 1.10 修正 JQuery標簽事件處理實例 如果你正在使用JQuery tabs而且想從基本的功能擴展到自定義的功能,這是你最好知道如何處理JQuery的點擊事件。 在這篇文章中:   1.回顧如何添加

css禁止文字選中

targe 內容 http www ondrag function -m 功能 中文 有時候,為了讓用戶有更好的體驗,需要禁用掉文本選中功能 比如:使用a標簽模擬按鈕,如果不禁用掉文本選中功能,那麽雙擊時會選中文字,用起來很不爽。 多數情況下,只需要使用CSS樣式就可以實現

js或css指定元素點擊時內容不可選中

指定元素 art class family adding input tro hover ria 一、css3中可以使用"user-select"屬性: body{ -webkit-user-select:none;/*谷歌

jquery判斷checkbox是否選中

fun utf-8 htm body meta inpu alert -c his <!doctype html> <html> <head> <meta charset="utf-8"> <title>無標題文

獲取select選中的option的值

選中項 nbsp 技術 技術分享 中項 cte ted com class <select id="select"> <option>綏江</option> <option>西江</option>

CSS.text不選中

table tab meta start values class ron 另一個 chrome 1、 text { -moz-user-select: none; /*火狐*/ -webkit-user-select: n

使用JQuery獲取選中的checkbox的value值

input checkbox meta div 意思 h+ javascrip 引入 html 上網查了一下,感覺一些人回答得真的是不知所雲,要麽代碼不夠簡便。或者是有些想裝逼成分等。 以下為使用JQuery獲取input checkbox被選中的值代碼: <html

vue中優化CheckBox初始狀態選中問題

checkbox end clas script author eal change click edit <template> <div class="hello"> <h2>我是主界面</h2>

SQL屬性第一個值不選中,屬性默認第一個值

http data- class col pro -o wrap gin 一個 把 Please Choose Color 屬性名設置為不可選的 UPDATE `products_attributes` SET `attributes_display_only` = ‘1‘

jq判斷複選框是否選中的3中方法

<input id="check1" type="checkbox" /> 方法一: var cbox = $("#check1").is(":checked"); alert(cbox); 複選框被選中彈出true,沒被選中彈出false。 方法二:

js中獲取選中radio的值

<input type="radio" name="address" value="0" /> <input type="radio" name="address" value="1" /> <input type="radio" name="address" va

jQuery中判斷input是否選中

<input type="checkbox" id="ipt" > <input type="checkbox" id="test" checked="checked"> var ck = $("#ipt").is(":checked"); // t

解決EasyUi中的DataGrid刪除一條記錄後,刪除的資料仍處於選中狀態問題

專案中遇到這麼一個問題,在easyui的datagrid中,刪除一條記錄成功,重新載入datagrid後,去點編輯操作,仍可把之前刪除掉的那條記錄進行編輯操作,正常情況應該是,刪除後再去點選“編輯”,應提示沒有被選中的記錄。現在這個情景說明,在datagrid刪除資料重新載入後,當datagrid呼叫getS

通過類或ID記錄上一個選中的標籤或者數字

 通過類或ID記錄上一個被選中的標籤或者數字 $(function () { $(document).on('click', '.navbar-nav li a', function (e) { var $parant = $('.navbar

java selenium判斷元素是否選中

java判斷元素是否被選中:  WebElement ele=driver.findElement(By.xpath(“//*[@id=’productAttributeType’]/option[3]”)); //獲得“數字”這個元素  Boolean flag=ele.isS