view.model()->data( index ).value< QString >();
view.setSelection( rect, QItemSelectionModel::ClearAndSelect );QModelIndexList indices_for_store = view.selectedIndexes();
QString textData; int rows = model->rowCount(); int columns = model->columnCount(); for (int i = 0; i < rows; i++) { textData += "| "; for (int j = 0; j < columns; j++) { textData += model->data(model->index(i,j)).toString(); textData += " | "; } textData += "\n"; } ui->textEdit->setText(textData); // .txt QFile txtFile("test.txt"); if(txtFile.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream out(&txtFile); out << textData.toLatin1(); txtFile.close(); }
Python# source: http://stackoverflow.com/a/5910078/5909792def ascii_table(rows): headers = rows[0] lens = list() for i in range(len(rows[0])): lens.append(len(max([x[i] for x in rows] + [headers[i]], key=lambda x: len(str(x))))) formats = list() hformats = list() for i in range(len(rows[0])): if isinstance(rows[0][i], int): formats.append("%%%dd" % lens[i]) else: formats.append("%%-%ds" % lens[i]) hformats.append("%%-%ds" % lens[i]) pattern = " | ".join(formats) hpattern = " | ".join(hformats) separator = "-+-".join(['-' * n for n in lens]) text_lines = [hpattern % tuple(headers), separator] for line in rows[1:]: text_lines.append(pattern % tuple(t for t in line)) return '\n'.join(text_lines)
Pythoncolumns = ['id', 'url', 'name', 'short_name', 'birthday', 'job', 'department', 'photo', 'work_phone', 'mobile_phone', 'email']rows = [['#1', 'http://amiller.example.com', 'Andrew Miller', 'amiller', '11 December', 'Testing Engineer', 'CD, Product Support Service', 'amiller.jpg', '', '', 'amiller@example.com'], ['#2', 'http://ataylor.example.com', 'Anthony Taylor', 'ataylor', '17 July', 'Software Engineer', 'CD, Product Support Service', 'ataylor.jpg', '', '', 'ataylor@example.com'], ['#3', 'http://dmoore.example.com', 'Daniel Moore', 'dmoore', '2 March', 'Testing Engineer', 'CD, Product Support Service', 'dmoore.jpg', '', '', 'dmoore@example.com'], ['#4', 'http://dsmith.example.com', 'David Smith', 'dsmith', '5 January', 'Testing Engineer', 'Processing Services Division', 'dsmith.jpg', '', '', 'dsmith@example.com'], ['#5', 'http://awilson.example.com', 'Alexander Wilson', 'awilson', '11 April', 'Software Engineer', 'CD, Product Support Service', 'awilson.jpg', '', '', 'awilson@example.com'], ['#6', 'http://asmith.example.com', 'Alexander Smith', 'asmith', '3 November', 'Testing Engineer', 'CD, Product Support Service', 'asmith.jpg', '', '', 'asmith@example.com'], ['#7', 'http://jdavis.example.com', 'Jayden Davis', 'jdavis', '10 April', 'Shift Engineer', 'BSD, Presale Solution Bureau', 'jdavis.jpg', '', '', 'jdavis@example.com'], ['#8', 'http://jdavis.example.com', 'Jacob Davis', 'jdavis', '5 July', 'Shift Engineer', 'DD, Technical Translation Bureau', 'jdavis.jpg', '', '', 'jdavis@example.com'], ['#9', 'http://ejones.example.com', 'Ethan Jones', 'ejones', '14 September', 'Software Engineer', 'BSD, Presale Solution Bureau', 'ejones.jpg', '', '', 'ejones@example.com'], ['#10', 'http://abrown.example.com', 'Angel Brown', 'abrown', '15 April', 'Testing Engineer', 'Processing Services Division', 'abrown.jpg', '', '', 'abrown@example.com'], ['#11', 'http://dwilliams.example.com', 'David Williams', 'dwilliams', '23 November', 'Application Developer', 'DD, Technical Translation Bureau', 'dwilliams.jpg', '', '', 'dwilliams@example.com'], ['#12', 'http://ddavis.example.com', 'Daniel Davis', 'ddavis', '5 September', 'Shift Engineer', 'DD, Technical Translation Bureau', 'ddavis.jpg', '', '', 'ddavis@example.com'], ['#13', 'http://ntaylor.example.com', 'Nathan Taylor', 'ntaylor', '16 February', 'Shift Engineer', 'BSD, Presale Solution Bureau', 'ntaylor.jpg', '', '', 'ntaylor@example.com'], ['#14', 'http://dtaylor.example.com', 'Daniel Taylor', 'dtaylor', '10 March', 'Application Developer', 'DD, Technical Translation Bureau', 'dtaylor.jpg', '', '', 'dtaylor@example.com'], ['#15', 'http://jbrown.example.com', 'Jayden Brown', 'jbrown', '1 January', 'Software Engineer', 'DD, Technical Translation Bureau', 'jbrown.jpg', '', '', 'jbrown@example.com'], ['#16', 'http://asmith.example.com', 'Andrew Smith', 'asmith', '9 August', 'Software Engineer', 'CD, Product Support Service', 'asmith.jpg', '', '', 'asmith@example.com'], ['#17', 'http://jsmith.example.com', 'Jayden Smith', 'jsmith', '8 April', 'Testing Engineer', 'BSD, Presale Solution Bureau', 'jsmith.jpg', '', '', 'jsmith@example.com'], ['#18', 'http://asmith.example.com', 'Alexander Smith', 'asmith', '7 November', 'Shift Engineer', 'Processing Services Division', 'asmith.jpg', '', '', 'asmith@example.com'], ['#19', 'http://dbrown.example.com', 'David Brown', 'dbrown', '20 October', 'Shift Engineer', 'Processing Services Division', 'dbrown.jpg', '', '', 'dbrown@example.com'], ['#20', 'http://nwilliams.example.com', 'Nathan Williams', 'nwilliams', '9 March', 'Shift Engineer', 'CD, Product Support Service', 'nwilliams.jpg', '', '', 'nwilliams@example.com'], ['#21', 'http://dtaylor.example.com', 'Daniel Taylor', 'dtaylor', '5 April', 'Shift Engineer', 'CD, Product Support Service', 'dtaylor.jpg', '', '', 'dtaylor@example.com'], ['#22', 'http://nmoore.example.com', 'Nathan Moore', 'nmoore', '14 July', 'Testing Engineer', 'DD, Technical Translation Bureau', 'nmoore.jpg', '', '', 'nmoore@example.com'], ['#23', 'http://ewilson.example.com', 'Ethan Wilson', 'ewilson', '7 September', 'Software Engineer', 'CD, Product Support Service', 'ewilson.jpg', '', '', 'ewilson@example.com'], ['#24', 'http://awilson.example.com', 'Angel Wilson', 'awilson', '5 December', 'Application Developer', 'BSD, Presale Solution Bureau', 'awilson.jpg', '', '', 'awilson@example.com'], ['#25', 'http://abrown.example.com', 'Angel Brown', 'abrown', '21 October', 'Shift Engineer', 'BSD, Presale Solution Bureau', 'abrown.jpg', '', '', 'abrown@example.com']] rows.insert(0, columns)print(ascii_table(rows))
id | url | name | short_name | birthday | job | department | photo | work_phone | mobile_phone | email ----+------------------------------+------------------+------------+--------------+-----------------------+----------------------------------+---------------+------------+--------------+----------------------#1 | http://amiller.example.com | Andrew Miller | amiller | 11 December | Testing Engineer | CD, Product Support Service | amiller.jpg | | | amiller@example.com #2 | http://ataylor.example.com | Anthony Taylor | ataylor | 17 July | Software Engineer | CD, Product Support Service | ataylor.jpg | | | ataylor@example.com #3 | http://dmoore.example.com | Daniel Moore | dmoore | 2 March | Testing Engineer | CD, Product Support Service | dmoore.jpg | | | dmoore@example.com #4 | http://dsmith.example.com | David Smith | dsmith | 5 January | Testing Engineer | Processing Services Division | dsmith.jpg | | | dsmith@example.com #5 | http://awilson.example.com | Alexander Wilson | awilson | 11 April | Software Engineer | CD, Product Support Service | awilson.jpg | | | awilson@example.com #6 | http://asmith.example.com | Alexander Smith | asmith | 3 November | Testing Engineer | CD, Product Support Service | asmith.jpg | | | asmith@example.com #7 | http://jdavis.example.com | Jayden Davis | jdavis | 10 April | Shift Engineer | BSD, Presale Solution Bureau | jdavis.jpg | | | jdavis@example.com #8 | http://jdavis.example.com | Jacob Davis | jdavis | 5 July | Shift Engineer | DD, Technical Translation Bureau | jdavis.jpg | | | jdavis@example.com #9 | http://ejones.example.com | Ethan Jones | ejones | 14 September | Software Engineer | BSD, Presale Solution Bureau | ejones.jpg | | | ejones@example.com #10 | http://abrown.example.com | Angel Brown | abrown | 15 April | Testing Engineer | Processing Services Division | abrown.jpg | | | abrown@example.com #11 | http://dwilliams.example.com | David Williams | dwilliams | 23 November | Application Developer | DD, Technical Translation Bureau | dwilliams.jpg | | | dwilliams@example.com#12 | http://ddavis.example.com | Daniel Davis | ddavis | 5 September | Shift Engineer | DD, Technical Translation Bureau | ddavis.jpg | | | ddavis@example.com #13 | http://ntaylor.example.com | Nathan Taylor | ntaylor | 16 February | Shift Engineer | BSD, Presale Solution Bureau | ntaylor.jpg | | | ntaylor@example.com #14 | http://dtaylor.example.com | Daniel Taylor | dtaylor | 10 March | Application Developer | DD, Technical Translation Bureau | dtaylor.jpg | | | dtaylor@example.com #15 | http://jbrown.example.com | Jayden Brown | jbrown | 1 January | Software Engineer | DD, Technical Translation Bureau | jbrown.jpg | | | jbrown@example.com #16 | http://asmith.example.com | Andrew Smith | asmith | 9 August | Software Engineer | CD, Product Support Service | asmith.jpg | | | asmith@example.com #17 | http://jsmith.example.com | Jayden Smith | jsmith | 8 April | Testing Engineer | BSD, Presale Solution Bureau | jsmith.jpg | | | jsmith@example.com #18 | http://asmith.example.com | Alexander Smith | asmith | 7 November | Shift Engineer | Processing Services Division | asmith.jpg | | | asmith@example.com #19 | http://dbrown.example.com | David Brown | dbrown | 20 October | Shift Engineer | Processing Services Division | dbrown.jpg | | | dbrown@example.com #20 | http://nwilliams.example.com | Nathan Williams | nwilliams | 9 March | Shift Engineer | CD, Product Support Service | nwilliams.jpg | | | nwilliams@example.com#21 | http://dtaylor.example.com | Daniel Taylor | dtaylor | 5 April | Shift Engineer | CD, Product Support Service | dtaylor.jpg | | | dtaylor@example.com #22 | http://nmoore.example.com | Nathan Moore | nmoore | 14 July | Testing Engineer | DD, Technical Translation Bureau | nmoore.jpg | | | nmoore@example.com #23 | http://ewilson.example.com | Ethan Wilson | ewilson | 7 September | Software Engineer | CD, Product Support Service | ewilson.jpg | | | ewilson@example.com #24 | http://awilson.example.com | Angel Wilson | awilson | 5 December | Application Developer | BSD, Presale Solution Bureau | awilson.jpg | | | awilson@example.com #25 | http://abrown.example.com | Angel Brown | abrown | 21 October | Shift Engineer | BSD, Presale Solution Bureau | abrown.jpg | | | abrown@example.com
C++ (Qt)QList<QStringList> rows;rows << (QStringList() << "ID" << "NAME" << "AGE");rows << (QStringList() << "1" << "Ivan Petrov" << "42");rows << (QStringList() << "2" << "LORD DESTRUCTOR" << "14");rows << (QStringList() << "3" << "Petya" << "22"); QString table = ascii_table(rows);qDebug().nospace().noquote() << table;
C++ (Qt) ID | NAME | AGE----+-----------------+---- 1 | Ivan Petrov | 42 2 | LORD DESTRUCTOR | 14 3 | Petya | 22
C++ (Qt)#include <QStringList>#include <QDebug> QString ascii_table(const QList<QStringList>& rows) { // Список максимальной ширины столбцов -- каждый элемент это отдельный столбец // А шириной будет считаться количество символов QList<int> lens; const QStringList& headers = rows[0]; // Перебор по столбцам for (int col = 0; col < headers.size(); col++) { int max_len = headers[col].size(); for (int row = 1; row < rows.size(); row++) { int cell_len = rows[row][col].size(); if (cell_len > max_len) { max_len = cell_len; } } lens.append(max_len); } // Генерация шаблона строк таблицы. // Если в lens = (2, 15, 3), тогда в formats = ("%-2s", "%-15s", "%-3s") // для QString.args = ("%1", "%2", "%3") QStringList formats; for (int i = 0; i < lens.size(); i++) { formats << QString("%%1").arg(i + 1); } QStringList column_separators; for (int col_len : lens) { column_separators.append(QString().fill('-', col_len)); } QString separators = column_separators.join("-+-"); QString pattern = formats.join(" | "); QString table; for (int row = 0; row < rows.size(); row++) { QString row_pattern = pattern; for (int col = 0; col < lens.size(); col++) { int col_len = lens[col]; QString cell = rows[row][col]; // -col_len -- т.е. выравнивание по левой стороне row_pattern = row_pattern.arg(cell, -col_len); } table += " " + row_pattern + "\n"; // TODO: Так лучше не делать, а разделить генерацию заголовка и тела таблицы if (row == 0) { table += "-" + separators + "\n"; } } return table;} int main(int argc, char *argv[]) { QList<QStringList> rows; rows << (QStringList() << "ID" << "NAME" << "AGE"); rows << (QStringList() << "1" << "Ivan Petrov" << "42"); rows << (QStringList() << "2" << "LORD DESTRUCTOR" << "14"); rows << (QStringList() << "3" << "Petya" << "22"); QString table = ascii_table(rows); qDebug().nospace().noquote() << table; return 0;}