void tst_QColumnView::indexAt() { QColumnView view; QCOMPARE(view.indexAt(QPoint(0,0)), QModelIndex()); QDirModel model; view.setModel(&model); QModelIndex home = model.index(QDir::homePath()); QModelIndex homeFile = model.index(0, 0, home); if (!homeFile.isValid()) return; view.setRootIndex(home); QRect rect = view.visualRect(QModelIndex()); QVERIFY(!rect.isValid()); rect = view.visualRect(homeFile); QVERIFY(rect.isValid()); QModelIndex child; for (int i = 0; i < model.rowCount(home); ++i) { child = model.index(i, 0, home); rect = view.visualRect(child); QVERIFY(rect.isValid()); if (i > 0) QVERIFY(rect.top() > 0); QCOMPARE(view.indexAt(rect.center()), child); view.selectionModel()->select(child, QItemSelectionModel::SelectCurrent); view.setCurrentIndex(child); qApp->processEvents(); QTest::qWait(200); // test that the second row doesn't start at 0 if (model.rowCount(child) > 0) { child = model.index(0, 0, child); QVERIFY(child.isValid()); rect = view.visualRect(child); QVERIFY(rect.isValid()); QVERIFY(rect.left() > 0); QCOMPARE(view.indexAt(rect.center()), child); break; } } }
void tst_QDirModel::filePath() { QFile::remove(SRCDIR "test.lnk"); QVERIFY(QFile(SRCDIR "tst_qdirmodel.cpp").link(SRCDIR "test.lnk")); QDirModel model; model.setResolveSymlinks(false); QModelIndex index = model.index(SRCDIR "test.lnk"); QVERIFY(index.isValid()); #ifndef Q_OS_WINCE QString path = SRCDIR; #else QString path = QFileInfo(SRCDIR).absoluteFilePath() + "/"; #endif QCOMPARE(model.filePath(index), path + QString( "test.lnk")); model.setResolveSymlinks(true); QCOMPARE(model.filePath(index), path + QString( "tst_qdirmodel.cpp")); QFile::remove(SRCDIR "test.lnk"); }
void tst_QDirModel::mkdir() { QFETCH(QString, dirName); QFETCH(bool, mkdirSuccess); QFETCH(int, rowCount); QDirModel model; model.setReadOnly(false); QModelIndex parent = model.index(SRCDIR "dirtest"); QVERIFY(parent.isValid()); QCOMPARE(model.rowCount(parent), 1); // start out with only 'test1' - in's in the depot QModelIndex index = model.mkdir(parent, dirName); bool success = index.isValid(); int rows = model.rowCount(parent); if (success && !model.rmdir(index)) QVERIFY(QDir(SRCDIR "dirtests").rmdir(dirName)); QCOMPARE(rows, rowCount); QCOMPARE(success, mkdirSuccess); }
void tst_QDirModel::rowsAboutToBeRemoved() { QFETCH(QString, test_path); QFETCH(QStringList, initial_files); QFETCH(int, remove_row); QFETCH(QStringList, remove_files); QFETCH(QStringList, expected_files); rowsAboutToBeRemoved_cleanup(test_path); // clean up first QVERIFY(rowsAboutToBeRemoved_init(test_path, initial_files)); QDirModel model; model.setReadOnly(false); qRegisterMetaType<QModelIndex>("QModelIndex"); // NOTE: QDirModel will call refresh() when a file is removed. refresh() will reread the entire directory, // and emit layoutAboutToBeChanged and layoutChange. So, instead of checking for // rowsAboutToBeRemoved/rowsRemoved we check for layoutAboutToBeChanged/layoutChanged QSignalSpy spy(&model, SIGNAL(layoutAboutToBeChanged())); QModelIndex parent = model.index(test_path); QVERIFY(parent.isValid()); // remove the file { QModelIndex index = model.index(remove_row, 0, parent); QVERIFY(index.isValid()); QVERIFY(model.remove(index)); } QCOMPARE(spy.count(), 1); // Compare the result for (int row = 0; row < expected_files.count(); ++row) { QModelIndex index = model.index(row, 0, parent); QString str = index.data().toString(); QCOMPARE(str, expected_files.at(row)); } QVERIFY(rowsAboutToBeRemoved_cleanup(test_path)); }
void tst_QColumnView::visualRegionForSelection() { ColumnView view; QItemSelection emptyItemSelection; QCOMPARE(QRegion(), view.visualRegionForSelection(emptyItemSelection)); // a region that isn't empty QDirModel model; view.setModel(&model); // On Windows CE the home directory might actually be empty. #ifndef Q_OS_WINCE QString location = QDir::homePath(); #else QString location = QLatin1String("/Windows"); #endif QModelIndex home = model.index(location); QVERIFY(model.rowCount(home) > 1); QItemSelection itemSelection(model.index(0, 0, home), model.index(model.rowCount(home) - 1, 0, home)); QVERIFY(QRegion() != view.visualRegionForSelection(itemSelection)); }
void tst_QDirModel::task244669_remove() { QFile f1(SRCDIR "dirtest/f1.txt"); QVERIFY(f1.open(QIODevice::WriteOnly)); f1.close(); QFile f2(SRCDIR "dirtest/f2.txt"); QVERIFY(f2.open(QIODevice::WriteOnly)); f2.close(); QDirModel model; model.setReadOnly(false); QPersistentModelIndex parent = model.index(SRCDIR "dirtest"); QPersistentModelIndex index2 = model.index(SRCDIR "dirtest/f2.txt"); QPersistentModelIndex index1 = model.index(SRCDIR "dirtest/f1.txt"); QVERIFY(parent.isValid()); QVERIFY(index1.isValid()); QVERIFY(index2.isValid()); QCOMPARE(parent.data() , model.index(SRCDIR "dirtest").data()); QCOMPARE(index1.data() , model.index(SRCDIR "dirtest/f1.txt").data()); QCOMPARE(index2.data() , model.index(SRCDIR "dirtest/f2.txt").data()); QVERIFY(model.remove(index1)); QVERIFY(parent.isValid()); QVERIFY(!index1.isValid()); QVERIFY(index2.isValid()); QCOMPARE(parent.data() , model.index(SRCDIR "dirtest").data()); QCOMPARE(index2.data() , model.index(SRCDIR "dirtest/f2.txt").data()); QVERIFY(model.remove(index2)); QVERIFY(parent.isValid()); QVERIFY(!index2.isValid()); QVERIFY(!index1.isValid()); QCOMPARE(parent.data() , model.index(SRCDIR "dirtest").data()); }
void tst_QColumnView::moveCursor() { QFETCH(bool, reverse); if (reverse) qApp->setLayoutDirection(Qt::RightToLeft); ColumnView view; // don't crash view.MoveCursor(ColumnView::MoveUp, Qt::NoModifier); // don't do anything QCOMPARE(view.MoveCursor(ColumnView::MoveEnd, Qt::NoModifier), QModelIndex()); QDirModel model; view.setModel(&model); QModelIndex home = model.index(QDir::homePath()); QModelIndex ci = view.currentIndex(); QCOMPARE(view.MoveCursor(ColumnView::MoveUp, Qt::NoModifier), QModelIndex()); QCOMPARE(view.MoveCursor(ColumnView::MoveDown, Qt::NoModifier), QModelIndex()); // left at root view.setCurrentIndex(model.index(0,0)); ColumnView::PublicCursorAction action = reverse ? ColumnView::MoveRight : ColumnView::MoveLeft; QCOMPARE(view.MoveCursor(action, Qt::NoModifier), model.index(0,0)); // left shouldn't move up int i = 0; ci = model.index(0, 0); while (i < model.rowCount() - 1 && !model.hasChildren(ci)) ci = model.index(++i, 0); QVERIFY(model.hasChildren(ci)); view.setCurrentIndex(ci); action = reverse ? ColumnView::MoveRight : ColumnView::MoveLeft; QCOMPARE(view.MoveCursor(action, Qt::NoModifier), ci); // now move to the left (i.e. move over one column) view.setCurrentIndex(home); QCOMPARE(view.MoveCursor(action, Qt::NoModifier), home.parent()); // right action = reverse ? ColumnView::MoveLeft : ColumnView::MoveRight; view.setCurrentIndex(ci); QModelIndex mc = view.MoveCursor(action, Qt::NoModifier); QCOMPARE(mc, model.index(0,0, ci)); // next one should move down QModelIndex idx = model.index(0, 0, ci); while (model.hasChildren(idx) && model.rowCount(ci) > idx.row() + 1) idx = idx.sibling(idx.row() + 1, idx.column()); view.setCurrentIndex(idx); mc = view.MoveCursor(action, Qt::NoModifier); QCOMPARE(mc, idx.sibling(idx.row() + 1, idx.column())); }
void Widget::on_pushButtonChIDV3_clicked() { ui->treeView->setSelectionMode(QTreeView::ExtendedSelection); ui->treeView->setSelectionBehavior(QTreeView::SelectRows); QModelIndexList list = ui->treeView->selectionModel()->selectedIndexes(); QDirModel* model = (QDirModel*)ui->treeView->model(); QModelIndex index = list[0]; QFileInfo fileInfo = model->fileInfo(index); QString selectedfilepath = fileInfo.absoluteFilePath(); qDebug() << selectedfilepath << '\n'; QSqlQuery query; //query.exec("INSERT INTO test VALUES('','sss','sss')"); //query.exec("SELECT * FROM test"); QString selectedfromdb = "SELECT absdirectory FROM test WHERE symlinkdirectory = '" + selectedfilepath + "'"; query.exec(selectedfromdb); query.next(); QString selectedasbd = query.value(0).toString(); qDebug() << selectedasbd << "\n"; FILE *pFile; //文件指针 Mp3Info song; QByteArray ba = selectedasbd.toAscii();//将所得文件名转换为字符//yfx0527 const char *dirnow = ba.data(); //printf("%s\n",dirnow); pFile = fopen(dirnow, "wb"); fseek(pFile, -125, SEEK_END); int i = 0; char title[30]; for(i = 0;i < 30;i ++) { title[i] = ' '; } char artist[30]; for(i = 0;i < 30;i ++) { artist[i] = ' '; } char album[30]; for(i = 0;i < 30;i ++) { album[i] = ' '; } char year[4]; for(i = 0;i < 4;i ++) { year[i] = ' '; } char comment[28]; for(i = 0;i < 28;i ++) { comment[i] = ' '; } QByteArray ba1; ba1 = ui->lineEditArtist->text().toAscii();//将所得文件名转换为字符//yfx0527 strcpy(artist,ba1.data()); ba1 = ui->lineEditTitle->text().toAscii();//将所得文件名转换为字符//yfx0527 strcpy(title,ba1.data()); ba1 = ui->lineEditAlbum->text().toAscii();//将所得文件名转换为字符//yfx0527 strcpy(album,ba1.data()); ba1 = ui->lineEditYear->text().toAscii();//将所得文件名转换为字符//yfx0527 strcpy(year,ba1.data()); ba1 = ui->lineEditComment->text().toAscii();//将所得文件名转换为字符//yfx0527 strcpy(comment,ba1.data()); char whatmm[3] = "XX"; fwrite(whatmm,sizeof(char),3,pFile); fwrite(title,sizeof(char),30,pFile); fwrite(artist,sizeof(char),30,pFile); fwrite(album,sizeof(char),30,pFile); fwrite(year,sizeof(char),4,pFile); fwrite(comment,sizeof(char),28,pFile); fclose(pFile); }
void Widget::on_pushButtonrefresh_clicked() { QDirModel *model = new QDirModel; ui->treeView->setModel(model); ui->treeView->setRootIndex(model->index(ui->lineEditmubiaomulu->text() + "/mp3")); }
void Widget::on_pushButtoncreate_clicked() { QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setDatabaseName("test"); db.setUserName("root"); db.setPassword("xtloving"); if (!db.open()) qDebug() << "Failed to connect to root mysql admin"; else std::cout<<"succeed!"<<std::endl; QSqlQuery query; query.exec("delete from test"); query.exec("SELECT * FROM test"); while(query.next()) { QString id = query.value(0).toString(); QString type = query.value(1).toString(); QString data = query.value(2).toString(); QString sqlout = id + "\n" + type + "\n" + data; //ui->listWidget->addItem(sqlout); } QString change1 = ui->lineEdityuanmulu->text(); QString change2 = ui->lineEditmubiaomulu->text() + "/mp3"; //const char* strchange1 = change1.toAscii().constData(); //const char* strchange2 = change2.toAscii().constData(); QByteArray ba = change1.toLatin1(); const char *strchange1 = ba.data(); //const char* strchange2 = change2.toAscii().constData(); QByteArray ba2 = change2.toLatin1(); const char *strchange2 = ba2.data(); scan_dir(strchange1, strchange2); fflush(stdout); /* char *dest = const_cast<char*>(strchange2); strcat(dest,"/mp3"); fd=watch_init(MASK,dest); watch_mon(fd); */ pid_t pid; if((pid=fork())<0) printf("Fork error!"); else if(pid==0){ int fd; char *dest = const_cast<char*>(strchange2); //strcat(dest,"/mp3"); fd=watch_init(MASK,dest); watch_mon(fd); } //ui->lineEditmubiaomulu->setText(ui->lineEditmubiaomulu->text() + "/mp3"); QDirModel *model = new QDirModel; ui->treeView->setModel(model); ui->treeView->setRootIndex(model->index(ui->lineEditmubiaomulu->text() + "/mp3")); }
void TemplateDialog::itemActivated(const QModelIndex & index) { QDirModel * model = qobject_cast<QDirModel*>(treeView->model()); if (model && !model->isDir(index)) accept(); }