上一页 下一页

KDE编程简介

Qt的例子

#include 
#include 

int main( int argc, char **argv )
{
    QApplication a( argc, argv );

    QPushButton *hello=new QPushButton( "Hello world!", 0 );
    hello->resize( 100, 30 );
 
    QObject::connect( hello, SIGNAL(clicked()), &a, SLOT(quit()) );
 
    a.setMainWidget( hello );
    hello->show();
 
    return a.exec();
} 

KDE的例子

#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
int main( int argc, char **argv )
{
    KApplication a( argc, argv, "example");
 
    MainWindow *window=new MainWindow( "Example" );
    window->resize( 400, 300 );
 
    a.setMainWidget( window );
    window->show();
 
    return a.exec();
} 

class MainWindow : public KTMainWindow
{
   Q_OBJECT
  public:
 
    MainWindow ( char * name );
 
  public slots:
    void fileOpen();
    void fileSave();
};
 
MainWindow::MainWindow ( char * name ) : KTMainWindow ( name )
{
    QPopupMenu *filemenu = new QPopupMenu;
    filemenu->insertItem( i18n( "&Open" ), this, SLOT(fileOpen()) );
    filemenu->insertItem( i18n( "&Save" ), this, SLOT(fileSave()) );
    filemenu->insertItem( i18n( "&Quit" ), kapp, SLOT(quit()) );
    QString about =
            i18n("This is an Example.\n"
                 "(c) 1999-2000 by Somebody.\n");
 
    QPopupMenu *helpmenu = helpMenu( about );
    KMenuBar *menu = new KMenuBar( this );
    menu->insertItem( i18n( "&File" ), filemenu );
    menu->insertSeparator();
    menu->insertItem( i18n( "&Help" ), helpmenu );
    setMenu( menu );
 
    QTextView *hello=new QTextView(
       i18n("Hello World !"), "", this );
    setView( hello );
 
} 
 
void MainWindow::fileOpen()
{
    QString tmpFile;
    
    KURL filename = KFileDialog::getOpenURL( QString::null, "*", this );
    if( KIO::NetAccess::download( filename.url(), tmpFile ) )
    {
     /* Do whatever you want with a _local_ file stored as tmpFile */
      
      KIO::NetAccess::removeTempFile( tmpFile );
    }

}
 
void MainWindow::fileSave()
{
   KURL filename=KFileDialog::getSaveURL( QString::null, "*", this );
}

DCOP的例子

DCOP requestor

    DCOPClient *client = kapp->dcopClient();
    client->attach();
    ByteArray params;
    QDataStream stream(params, IO_WriteOnly);
    stream << location->text();
    if (!client->send("Servant-*", "bookmarkList", "add(QString)", params))
       kDebugError( "Error with DCOP");

DCOP Servant

   DCOPClient *client=kapp->dcopClient();
   client->attach();
   client->registerAs("Servant");
   void MainList::add( QString s )
	{
   		insertItem ( new QListViewItem ( this , s ) );
	}; 

Interface定义

#include 
#include 
 
class Iface : virtual public DCOPObject
{
    K_DCOP
public:
 
k_dcop:
    virtual void add( QString s ) = 0;
 
};

头文件定义

class MainList : public QListView, virtual public Iface
{
 Q_OBJECT
 
public:
 
 MainList();
 
 void add ( QString s );
 
};
 

Scripting

#!/usr/bin/python
 
from xmlrpclib import *
import os
 
rc = open(os.environ['HOME'] + '/.kxmlrpcd', 'r')
config = string.split(rc.read(), ',')
port = config[0]
auth = config[1]
 
server = Server("http://localhost:" + port +"/p6")
 
server.bookmarkList.add(auth, "http://www.kde.org")  

上一页 下一页