I seem to have hit another snag, this time to do with the connections of the tables Cells. im trying to set up a connection with the hopes of each time a cell is clicked on (or navigated to via the arrow keys), so I'm guessing a cellPressed(int, int) would be suitable, although Qt doesn't seem to think so...
When I run the program, the list populates properly, but when I click on a cell, regardless on if it's empty or not (which when it first loads up, it is empty), it will throw an unhandled exception:
Unhandled exception at 0x657d748c (QtGuid4.dll) in BatchRenamer.exe: 0xC0000005: Access violation reading location 0x00000000.
Then it will throw me out at line 108 in qtablewidget.h
{ return data(Qt::DisplayRole).toString(); }
inline QString text() const
{ return data(Qt::DisplayRole).toString(); }
To copy to clipboard, switch view to plain text mode
Now if I had to guess, it would be due to the fact that there's nothing in the cell, probably not even an empty string, but I'm not too sure how I can handle and catch this exception.
Here's the code that sets up the connection:
void BatchRenamer
::populateTable( QString folderName
) {
// no need to validate folderName because it's already done in openFolder()
QDir activeDirectory
( folderName
);
// clear the current file list before doing anything
ui.tblFiles->clearContents();
disconnect( ui.tblFiles );
if ( !oldFileList.isEmpty() )
{
for ( int i = 0 ; i < oldFileList.size() ; i++ )
{
ui.tblFiles->removeRow(i);
ui.tblFiles->hideRow(i);
}
}
if ( !fileList.isEmpty() )
{
for ( int i = 0; i < fileList.size() ; i++ )
{
// make the items in the first column read only
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
ui.tblFiles->setRowCount( fileList.count() );
ui.tblFiles->setItem( i, 0, item );
ui.tblFiles->showRow( i );
}
}
ui.tblFiles->resizeColumnToContents( 0 );
oldFileList = fileList;
// hook up the newly made table entries
connect( ui.tblFiles, SIGNAL( cellChanged(int, int)), this, SLOT( changeLocalSongData( int, int )));
connect( ui.tblFiles, SIGNAL( cellPressed(int, int)), this, SLOT( changeLocalSongData( int, int )));
}
void BatchRenamer::populateTable( QString folderName )
{
// no need to validate folderName because it's already done in openFolder()
QDir activeDirectory( folderName );
QStringList fileList = activeDirectory.entryList( QStringList() << "*.mp3" << "*.wma", QDir::Files );
// clear the current file list before doing anything
ui.tblFiles->clearContents();
disconnect( ui.tblFiles );
if ( !oldFileList.isEmpty() )
{
for ( int i = 0 ; i < oldFileList.size() ; i++ )
{
ui.tblFiles->removeRow(i);
ui.tblFiles->hideRow(i);
}
}
if ( !fileList.isEmpty() )
{
for ( int i = 0; i < fileList.size() ; i++ )
{
QTableWidgetItem* item = new QTableWidgetItem( fileList.at(i) );
// make the items in the first column read only
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
ui.tblFiles->setRowCount( fileList.count() );
ui.tblFiles->setItem( i, 0, item );
ui.tblFiles->showRow( i );
}
}
ui.tblFiles->resizeColumnToContents( 0 );
oldFileList = fileList;
// hook up the newly made table entries
connect( ui.tblFiles, SIGNAL( cellChanged(int, int)), this, SLOT( changeLocalSongData( int, int )));
connect( ui.tblFiles, SIGNAL( cellPressed(int, int)), this, SLOT( changeLocalSongData( int, int )));
}
To copy to clipboard, switch view to plain text mode
Here's the changeLocalSongData(int,int) code
void BatchRenamer::changeLocalSongData( int row, int column )
{
if( column == 1)
{
songName = ui.tblFiles->item(row, column)->text();
updatePreview();
}
}
void BatchRenamer::changeLocalSongData( int row, int column )
{
if( column == 1)
{
songName = ui.tblFiles->item(row, column)->text();
updatePreview();
}
}
To copy to clipboard, switch view to plain text mode
And it may be pointless to do so, but here's the updatePreview() function (all this is really to display some hidden information down the bottom of the window (under the "Preview:" label in the screenshot above).
void BatchRenamer::updatePreview()
{
if ( artistName != "")
{
previewText += artistName;
}
if (albumTitle != "")
{
previewText += spacer + albumTitle;
}
if (songName != "")
{
previewText += spacer + songName;
}
ui.lblPreviewFileName->setText( previewText );
}
void BatchRenamer::updatePreview()
{
QString previewText = "\t";
if ( artistName != "")
{
previewText += artistName;
}
if (albumTitle != "")
{
previewText += spacer + albumTitle;
}
if (songName != "")
{
previewText += spacer + songName;
}
ui.lblPreviewFileName->setText( previewText );
}
To copy to clipboard, switch view to plain text mode
I've already tried catching the exception and handling it in the changeLocalSongData() function via this code:
if (ui.tblFiles->item(row,column)->text() != "")
{
songName = ui.tblFiles->item(row, column)->text();
updatePreview();
}
else
ui.tblFiles->item(row,column)->setText("");
if (ui.tblFiles->item(row,column)->text() != "")
{
songName = ui.tblFiles->item(row, column)->text();
updatePreview();
}
else
ui.tblFiles->item(row,column)->setText("");
To copy to clipboard, switch view to plain text mode
I have no doubt that this is a PEBKAC (problem existing between keyboard and char) error.
I would appreciate any help.
Thanks in advance.
P.S. If I comment out the connection to cellPressed(), the cellChanged connection will work without a hitch.
Bookmarks