Tabelleninhalt mit QT abspeichern

homoeolin

homoeolin

QT-Freak
Hallo,

wie speichert man den Inhalt einer
Tabelle unter QT ab und wie öffnet man diesen dann?
Das Abspeichern soll in eine Textdatei
oder eine andere Datei erfolgen.

Ich hoffe ihr könnt mir weiterhelfen.

Tobias
 
schau dir mal QFile und QTextStream an

Code:
QFile datei(argv[1]);

QTextStream outStream(&datei, IO_Write_Only);

nur als Ansatz z.B:
für jede zeile jede spalte auslesen und ab nach outStream
Code:
for(int i=0; i<numRows(); ++i)
{
	for(int j=0; j<numCols(); ++j)
	{
		QString s;
		s = text(i, j);
		outStream << s << " ";
	}
		// eine Zeile weiter
		outStream << endl;
}
 
Probleme bei den numROWS und numCOLS-Variablen

Danke für den Tip ! ;)
Aber nun erhalte ich folgende Meldung beim compilieren:

g++ -c -pipe -Wall -W -O2 -DNO_DEBUG -I/usr/local/qt/include -I/usr/local/qt/include -o main.o main.cpp
main.cpp: In function `void save()':
main.cpp:49: `500' cannot be used as a function
main.cpp:51: `4' cannot be used as a function
main.cpp:54: implicit declaration of function `int text(...)'
main.cpp:55: `outStream' undeclared (first use this function)
main.cpp:55: (Each undeclared identifier is reported only once
main.cpp:55: for each function it appears in.)
make: *** [main.o] Fehler 1

Die 4 und 500 sind die numROWS und numCOLS-Variablen.
Im Code werden diese wie folgt eingefügt:

const int numRows = 500; // Tablesize: number of rows
const int numCols = 4; // Tablesize: number of columns

Ich hoffe ihr könnt mir helfen ...
:]

Tobias

Wenn ihr euch über den Quellcode stürtzen wollt (er ist noch sehr primitiv...), liegt er als Anhang vor !
 

Anhänge

  • tabellen.zip
    704 Bytes · Aufrufe: 4
hallo

1.)
du mußt die Variablen "numCols" und "numRows" umbenennen, da es eine gleichnamige Funktion von QTable gibt.

Gebraucht werden sie allerdings nicht.

Oder du schreibst in void save():
Code:
QTable::numCols()
QTable::numRows()

2.)

outStream ist nicht deklariert
Code:
QTextStream outStream("&datei, IO_Write_Only);

vorher muß natürlich die Datei geöffnet werden, wo outStream hinschreibt:
Code:
QFile datei(argv[1]);
 
hier mal ein kleines Beispiel, mit KDevelop erstellt:

mytable.h
Code:
#ifndef MYTABLE_H
#define MYTABLE_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <kapp.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qtable.h>
#include <qfile.h>
#include <qtextstream.h>

/** MyTable is the base class of the project */
class MyTable : public QWidget
{
  Q_OBJECT 
  public:
    /** construtor */
    MyTable(QWidget* parent=0, const char *name=0);
    /** destructor */
    ~MyTable();


  public slots:
    void slot_save();

  private:
    int cols;
    int rows;
    QPushButton* savep;
    QTable* table;
};

#endif

mytable.cpp
Code:
#include "mytable.h"

MyTable::MyTable(QWidget *parent, const char *name) : QWidget(parent, name)
{
  setGeometry(10, 10,520,360);
  
  rows=20;
  cols=4;  
  table = new QTable(rows, cols, this);
  table->setGeometry(10,10,500,300);

  savep = new QPushButton("Save" , this);
  savep->setGeometry(10, 320, 500, 30);

  QObject::connect(savep, SIGNAL( clicked() ), this, SLOT( slot_save() ));
  
}

MyTable::~MyTable()
{
}


void MyTable::slot_save()
{
  QFile file("file.txt");
  file.open(IO_WriteOnly);
  QTextStream outStream(&file);

  for(int i=0; i< table->numRows(); ++i)
  {
    for(int j=0; j< table->numCols(); ++j)
    {
       QString s;
       s = table->text(i, j);
       outStream << s << " ";
    }
    // eine Zeile weiter
    outStream << endl;
  }
}

main.cpp
[code]
#include <kcmdlineargs.h>
#include <kaboutdata.h>
#include <klocale.h>

#include "mytable.h"

static const char *description =
	I18N_NOOP("MyTable");
// INSERT A DESCRIPTION FOR YOUR APPLICATION HERE
	
	
static KCmdLineOptions options[] =
{
  { 0, 0, 0 }
  // INSERT YOUR COMMANDLINE OPTIONS HERE
};

int main(int argc, char *argv[])
{

  KAboutData aboutData( "mytable", I18N_NOOP("MyTable"),
    VERSION, description, KAboutData::License_GPL,
    "(c) 2002, mapiox", 0, 0, "mapiox@unixboard.de");
  aboutData.addAuthor("mapiox",0, "mapiox@unixboard.de");
  KCmdLineArgs::init( argc, argv, &aboutData );
  KCmdLineArgs::addCmdLineOptions( options ); // Add our own options.

  KApplication a;
  MyTable *mytable = new MyTable();
  a.setMainWidget(mytable);
  mytable->show();  

  return a.exec();
}

das sollte dir weiterhelfen
 

Ähnliche Themen

Verzeichnis mit 1200 Dateien auf Verweise in Textdateien checken

Wie sende ich eine Datei von Linux an einen eingesteckten Datenträger?

Nginx als Reverse Proxy für Nextcloud und Emby

Spalten einer Datei in neue Datei integrieren.

Mit AWK verschiedene Felder verschiedener Zeilen vergleichen

Zurück
Oben