Fragen:
Wie finde ich das "aktive" QLineEdit heraus?
Wie erhalte ich die aktuelle Cursor Position?
Wie haenge ich es an den Text an der aktuellen Cursorposition an?
Ich habe folgendes Beispiel dazu geschrieben:
main.cpp
Code: Alles auswählen
#include "Toolbar.hpp"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Toolbar* toolbar = new Toolbar();
toolbar->show();
return app.exec();
};
Code: Alles auswählen
#ifndef TOOLBAR
#define TOOLBAR
#include <QtGui>
#include <iostream>
class Toolbar : public QMainWindow
{
Q_OBJECT
private slots:
void slotInsertLetter(){
QString szLetter = "";
QAction *action = qobject_cast< QAction* >(sender());
if(action) szLetter = action->data().toString();
// FIXME
std::cout << "szLetter ist \""<< szLetter.toStdString()
<< "\" und sollte nun an genau an die Stelle gesetzt werden,"
<< "wo der Curser gerade steht!\n";
};
private:
QAction *act1, *act2, *act3;
QLineEdit *le1, *le2;
QToolBar *tb;
void initGUI(){
le1 = new QLineEdit();
le2 = new QLineEdit();
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(le1);
layout->addWidget(le2);
setCentralWidget(new QWidget());
centralWidget()->setLayout(layout);
};
void createActions(){
act1 = new QAction(tr("ä"), this);
act1->setData("ä");
connect(act1, SIGNAL(triggered()), this, SLOT(slotInsertLetter()));
act2 = new QAction(tr("ö"), this);
act2->setData("ö");
connect(act2, SIGNAL(triggered()), this, SLOT(slotInsertLetter()));
act3 = new QAction(tr("ü"), this);
act3->setData("ü");
connect(act3, SIGNAL(triggered()), this, SLOT(slotInsertLetter()));
};
void createToolBar(){
tb = addToolBar(tr("letters"));
tb->addAction(act1);
tb->addAction(act2);
tb->addAction(act3);
};
public:
Toolbar(){
initGUI();
createActions();
createToolBar();
};
};
#endif